agarmgas.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. {
  2. $Id$
  3. Copyright (c) 2003 by Florian Klaempfl
  4. This unit implements an asm for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the GNU Assembler writer for the ARM
  19. }
  20. unit agarmgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmtai,
  25. aggas,
  26. cpubase;
  27. type
  28. PARMGNUAssembler=^TARMGNUAssembler;
  29. TARMGNUAssembler=class(TGNUassembler)
  30. procedure WriteInstruction(hp : tai);override;
  31. end;
  32. const
  33. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  34. '','lsl','lsr','asr','ror','rrx');
  35. implementation
  36. uses
  37. cutils,globals,verbose,
  38. systems,
  39. assemble,
  40. aasmcpu,
  41. itcpugas,
  42. cgbase,cgutils;
  43. const
  44. as_arm_gas_info : tasminfo =
  45. (
  46. id : as_gas;
  47. idtxt : 'AS';
  48. asmbin : 'as';
  49. asmcmd : '-o $OBJ $ASM';
  50. supported_target : system_any;
  51. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  52. labelprefix : '.L';
  53. comment : '# ';
  54. );
  55. function getreferencestring(var ref : treference) : string;
  56. var
  57. s : string;
  58. begin
  59. with ref do
  60. begin
  61. {$ifdef extdebug}
  62. // if base=NR_NO then
  63. // internalerror(200308292);
  64. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  65. // internalerror(200308293);
  66. {$endif extdebug}
  67. if assigned(symbol) then
  68. begin
  69. if (base<>NR_NO) and not(is_pc(base)) then
  70. internalerror(200309011);
  71. s:=symbol.name;
  72. if offset<0 then
  73. s:=s+tostr(offset)
  74. else if offset>0 then
  75. s:=s+'+'+tostr(offset);
  76. end
  77. else
  78. begin
  79. s:='['+gas_regname(base);
  80. if addressmode=AM_POSTINDEXED then
  81. s:=s+']';
  82. if index<>NR_NO then
  83. begin
  84. if signindex<0 then
  85. s:=s+', -'
  86. else
  87. s:=s+', ';
  88. s:=s+gas_regname(index);
  89. if shiftmode<>SM_None then
  90. s:=s+' ,'+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  91. end
  92. else if offset<>0 then
  93. s:=s+', #'+tostr(offset);
  94. case addressmode of
  95. AM_OFFSET:
  96. s:=s+']';
  97. AM_PREINDEXED:
  98. s:=s+']!';
  99. end;
  100. end;
  101. end;
  102. getreferencestring:=s;
  103. end;
  104. const
  105. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  106. function getopstr(const o:toper) : string;
  107. var
  108. hs : string;
  109. first : boolean;
  110. r : tsuperregister;
  111. begin
  112. case o.typ of
  113. top_reg:
  114. getopstr:=gas_regname(o.reg);
  115. top_shifterop:
  116. begin
  117. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  118. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  119. else if (o.shifterop^.rs=NR_NO) then
  120. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  121. else internalerror(200308282);
  122. end;
  123. top_const:
  124. getopstr:='#'+tostr(longint(o.val));
  125. top_regset:
  126. begin
  127. getopstr:='{';
  128. first:=true;
  129. for r:=RS_R0 to RS_R15 do
  130. if r in o.regset^ then
  131. begin
  132. if not(first) then
  133. getopstr:=getopstr+',';
  134. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  135. first:=false;
  136. end;
  137. getopstr:=getopstr+'}';
  138. end;
  139. top_ref:
  140. if o.ref^.refaddr=addr_full then
  141. begin
  142. hs:=o.ref^.symbol.name;
  143. if o.ref^.offset>0 then
  144. hs:=hs+'+'+tostr(o.ref^.offset)
  145. else
  146. if o.ref^.offset<0 then
  147. hs:=hs+tostr(o.ref^.offset);
  148. getopstr:=hs;
  149. end
  150. else
  151. getopstr:=getreferencestring(o.ref^);
  152. else
  153. internalerror(2002070604);
  154. end;
  155. end;
  156. Procedure TARMGNUAssembler.WriteInstruction(hp : tai);
  157. var op: TAsmOp;
  158. s: string;
  159. i: byte;
  160. sep: string[3];
  161. begin
  162. op:=taicpu(hp).opcode;
  163. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  164. if taicpu(hp).ops<>0 then
  165. begin
  166. sep:=#9;
  167. for i:=0 to taicpu(hp).ops-1 do
  168. begin
  169. // debug code
  170. // writeln(s);
  171. // writeln(taicpu(hp).fileinfo.line);
  172. { LDM and STM use references as first operand but they are written like a register }
  173. if (i=0) and (op in [A_LDM,A_STM]) then
  174. begin
  175. case taicpu(hp).oper[0]^.typ of
  176. top_ref:
  177. begin
  178. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  179. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  180. s:=s+'!';
  181. end;
  182. top_reg:
  183. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  184. else
  185. internalerror(200311292);
  186. end;
  187. end
  188. { register count of SFM and LFM is written without # }
  189. else if (i=1) and (op in [A_SFM,A_LFM]) then
  190. begin
  191. case taicpu(hp).oper[1]^.typ of
  192. top_const:
  193. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  194. else
  195. internalerror(200311292);
  196. end;
  197. end
  198. else
  199. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  200. sep:=',';
  201. end;
  202. end;
  203. AsmWriteLn(s);
  204. end;
  205. begin
  206. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  207. end.
  208. {
  209. $Log$
  210. Revision 1.23 2004-11-06 17:44:47 florian
  211. + additional extdebug check for wrong add_reg_instructions added
  212. * too long manglednames are cut off at 200 chars using a crc
  213. Revision 1.22 2004/11/01 17:41:28 florian
  214. * fixed arm compilation with cgutils
  215. * ...
  216. Revision 1.21 2004/06/20 08:55:31 florian
  217. * logs truncated
  218. Revision 1.20 2004/06/16 20:07:10 florian
  219. * dwarf branch merged
  220. Revision 1.19.2.1 2004/06/12 17:01:01 florian
  221. * fixed compilation of arm compiler
  222. Revision 1.19 2004/03/29 19:19:35 florian
  223. + arm floating point register saving implemented
  224. * hopefully stabs generation for MacOSX fixed
  225. + some defines for arm added
  226. Revision 1.18 2004/03/06 20:35:19 florian
  227. * fixed arm compilation
  228. * cleaned up code generation for exported linux procedures
  229. }