agarmgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the GNU Assembler writer for the ARM
  18. }
  19. unit agarmgas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. aasmtai,aasmdata,
  25. aggas,
  26. cpubase;
  27. type
  28. TARMGNUAssembler=class(TGNUassembler)
  29. constructor create(smart: boolean); override;
  30. function MakeCmdLine: TCmdStr; override;
  31. procedure WriteExtraHeader; override;
  32. end;
  33. TArmInstrWriter=class(TCPUInstrWriter)
  34. procedure WriteInstruction(hp : tai);override;
  35. end;
  36. TArmAppleGNUAssembler=class(TAppleGNUassembler)
  37. constructor create(smart: boolean); override;
  38. end;
  39. const
  40. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  41. '','lsl','lsr','asr','ror','rrx');
  42. implementation
  43. uses
  44. cutils,globals,verbose,
  45. systems,
  46. assemble,
  47. cpuinfo,aasmcpu,
  48. itcpugas,
  49. cgbase,cgutils;
  50. {****************************************************************************}
  51. { GNU Arm Assembler writer }
  52. {****************************************************************************}
  53. constructor TArmGNUAssembler.create(smart: boolean);
  54. begin
  55. inherited create(smart);
  56. InstrWriter := TArmInstrWriter.create(self);
  57. end;
  58. function TArmGNUAssembler.MakeCmdLine: TCmdStr;
  59. begin
  60. result:=inherited MakeCmdLine;
  61. if (current_settings.fputype = fpu_soft) then
  62. result:='-mfpu=softvfp '+result;
  63. if (current_settings.fputype = fpu_vfpv2) then
  64. result:='-mfpu=vfpv2 '+result;
  65. if (current_settings.fputype = fpu_vfpv3) then
  66. result:='-mfpu=vfpv3 '+result;
  67. if (current_settings.fputype = fpu_vfpv3_d16) then
  68. result:='-mfpu=vfpv3-d16 '+result;
  69. if current_settings.cputype = cpu_armv7m then
  70. result:='-march=armv7m -mthumb -mthumb-interwork '+result;
  71. if target_info.abi = abi_eabihf then
  72. { options based on what gcc uses on debian armhf }
  73. result:='-mfloat-abi=hard -meabi=5 '+result;
  74. end;
  75. procedure TArmGNUAssembler.WriteExtraHeader;
  76. begin
  77. inherited WriteExtraHeader;
  78. if current_settings.cputype in cpu_thumb2 then
  79. AsmWriteLn(#9'.syntax unified');
  80. end;
  81. {****************************************************************************}
  82. { GNU/Apple ARM Assembler writer }
  83. {****************************************************************************}
  84. constructor TArmAppleGNUAssembler.create(smart: boolean);
  85. begin
  86. inherited create(smart);
  87. InstrWriter := TArmInstrWriter.create(self);
  88. end;
  89. {****************************************************************************}
  90. { Helper routines for Instruction Writer }
  91. {****************************************************************************}
  92. function getreferencestring(var ref : treference) : string;
  93. var
  94. s : string;
  95. begin
  96. with ref do
  97. begin
  98. {$ifdef extdebug}
  99. // if base=NR_NO then
  100. // internalerror(200308292);
  101. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  102. // internalerror(200308293);
  103. {$endif extdebug}
  104. if assigned(symbol) then
  105. begin
  106. if (base<>NR_NO) and not(is_pc(base)) then
  107. internalerror(200309011);
  108. s:=symbol.name;
  109. if offset<0 then
  110. s:=s+tostr(offset)
  111. else if offset>0 then
  112. s:=s+'+'+tostr(offset);
  113. end
  114. else
  115. begin
  116. s:='['+gas_regname(base);
  117. if addressmode=AM_POSTINDEXED then
  118. s:=s+']';
  119. if index<>NR_NO then
  120. begin
  121. if signindex<0 then
  122. s:=s+', -'
  123. else
  124. s:=s+', ';
  125. s:=s+gas_regname(index);
  126. if shiftmode<>SM_None then
  127. s:=s+', '+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  128. end
  129. else if offset<>0 then
  130. s:=s+', #'+tostr(offset);
  131. case addressmode of
  132. AM_OFFSET:
  133. s:=s+']';
  134. AM_PREINDEXED:
  135. s:=s+']!';
  136. end;
  137. end;
  138. end;
  139. getreferencestring:=s;
  140. end;
  141. const
  142. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  143. function getopstr(const o:toper) : string;
  144. var
  145. hs : string;
  146. first : boolean;
  147. r : tsuperregister;
  148. begin
  149. case o.typ of
  150. top_reg:
  151. getopstr:=gas_regname(o.reg);
  152. top_shifterop:
  153. begin
  154. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  155. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  156. else if (o.shifterop^.rs=NR_NO) then
  157. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  158. else internalerror(200308282);
  159. end;
  160. top_const:
  161. getopstr:='#'+tostr(longint(o.val));
  162. top_regset:
  163. begin
  164. getopstr:='{';
  165. first:=true;
  166. for r:=RS_R0 to RS_R15 do
  167. if r in o.regset^ then
  168. begin
  169. if not(first) then
  170. getopstr:=getopstr+',';
  171. getopstr:=getopstr+gas_regname(newreg(o.regtyp,r,o.subreg));
  172. first:=false;
  173. end;
  174. getopstr:=getopstr+'}';
  175. end;
  176. top_conditioncode:
  177. getopstr:=cond2str[o.cc];
  178. top_modeflags:
  179. begin
  180. getopstr:='';
  181. if mfA in o.modeflags then getopstr:=getopstr+'a';
  182. if mfI in o.modeflags then getopstr:=getopstr+'i';
  183. if mfF in o.modeflags then getopstr:=getopstr+'f';
  184. end;
  185. top_ref:
  186. if o.ref^.refaddr=addr_full then
  187. begin
  188. hs:=o.ref^.symbol.name;
  189. if o.ref^.offset>0 then
  190. hs:=hs+'+'+tostr(o.ref^.offset)
  191. else
  192. if o.ref^.offset<0 then
  193. hs:=hs+tostr(o.ref^.offset);
  194. getopstr:=hs;
  195. end
  196. else
  197. getopstr:=getreferencestring(o.ref^);
  198. else
  199. internalerror(2002070604);
  200. end;
  201. end;
  202. Procedure TArmInstrWriter.WriteInstruction(hp : tai);
  203. var op: TAsmOp;
  204. postfix,s: string;
  205. i: byte;
  206. sep: string[3];
  207. begin
  208. op:=taicpu(hp).opcode;
  209. if current_settings.cputype in cpu_thumb2 then
  210. begin
  211. postfix:='';
  212. if taicpu(hp).wideformat then
  213. postfix:='.w';
  214. if taicpu(hp).ops = 0 then
  215. s:=#9+gas_op2str[op]+' '+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix]
  216. else
  217. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix]+postfix+cond2str[taicpu(hp).condition]; // Conditional infixes are deprecated in unified syntax
  218. end
  219. else
  220. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  221. if taicpu(hp).ops<>0 then
  222. begin
  223. sep:=#9;
  224. for i:=0 to taicpu(hp).ops-1 do
  225. begin
  226. // debug code
  227. // writeln(s);
  228. // writeln(taicpu(hp).fileinfo.line);
  229. { LDM and STM use references as first operand but they are written like a register }
  230. if (i=0) and (op in [A_LDM,A_STM,A_FSTM,A_FLDM]) then
  231. begin
  232. case taicpu(hp).oper[0]^.typ of
  233. top_ref:
  234. begin
  235. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  236. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  237. s:=s+'!';
  238. end;
  239. top_reg:
  240. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  241. else
  242. internalerror(200311292);
  243. end;
  244. end
  245. { register count of SFM and LFM is written without # }
  246. else if (i=1) and (op in [A_SFM,A_LFM]) then
  247. begin
  248. case taicpu(hp).oper[1]^.typ of
  249. top_const:
  250. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  251. else
  252. internalerror(200311292);
  253. end;
  254. end
  255. else
  256. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  257. sep:=',';
  258. end;
  259. end;
  260. owner.AsmWriteLn(s);
  261. end;
  262. const
  263. as_arm_gas_info : tasminfo =
  264. (
  265. id : as_gas;
  266. idtxt : 'AS';
  267. asmbin : 'as';
  268. asmcmd : '-o $OBJ $ASM';
  269. supported_targets : [system_arm_linux,system_arm_wince,system_arm_gba,system_arm_palmos,system_arm_nds,system_arm_embedded,system_arm_symbian];
  270. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  271. labelprefix : '.L';
  272. comment : '# ';
  273. dollarsign: '$';
  274. );
  275. as_arm_gas_darwin_info : tasminfo =
  276. (
  277. id : as_darwin;
  278. idtxt : 'AS-Darwin';
  279. asmbin : 'as';
  280. asmcmd : '-o $OBJ $ASM -arch $ARCH';
  281. supported_targets : [system_arm_darwin];
  282. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  283. labelprefix : 'L';
  284. comment : '# ';
  285. dollarsign: '$';
  286. );
  287. begin
  288. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  289. RegisterAssembler(as_arm_gas_darwin_info,TArmAppleGNUAssembler);
  290. end.