agarmgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. else if current_settings.cputype=cpu_armv6 then
  72. result:='-march=armv6 '+result
  73. else if current_settings.cputype=cpu_armv7 then
  74. result:='-march=armv7-a '+result;
  75. if target_info.abi = abi_eabihf then
  76. { options based on what gcc uses on debian armhf }
  77. result:='-mfloat-abi=hard -meabi=5 '+result;
  78. end;
  79. procedure TArmGNUAssembler.WriteExtraHeader;
  80. begin
  81. inherited WriteExtraHeader;
  82. if current_settings.cputype in cpu_thumb2 then
  83. AsmWriteLn(#9'.syntax unified');
  84. end;
  85. {****************************************************************************}
  86. { GNU/Apple ARM Assembler writer }
  87. {****************************************************************************}
  88. constructor TArmAppleGNUAssembler.create(smart: boolean);
  89. begin
  90. inherited create(smart);
  91. InstrWriter := TArmInstrWriter.create(self);
  92. end;
  93. {****************************************************************************}
  94. { Helper routines for Instruction Writer }
  95. {****************************************************************************}
  96. function getreferencestring(var ref : treference) : string;
  97. var
  98. s : string;
  99. begin
  100. with ref do
  101. begin
  102. {$ifdef extdebug}
  103. // if base=NR_NO then
  104. // internalerror(200308292);
  105. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  106. // internalerror(200308293);
  107. {$endif extdebug}
  108. if assigned(symbol) then
  109. begin
  110. if (base<>NR_NO) and not(is_pc(base)) then
  111. internalerror(200309011);
  112. s:=symbol.name;
  113. if offset<0 then
  114. s:=s+tostr(offset)
  115. else if offset>0 then
  116. s:=s+'+'+tostr(offset);
  117. end
  118. else
  119. begin
  120. s:='['+gas_regname(base);
  121. if addressmode=AM_POSTINDEXED then
  122. s:=s+']';
  123. if index<>NR_NO then
  124. begin
  125. if signindex<0 then
  126. s:=s+', -'
  127. else
  128. s:=s+', ';
  129. s:=s+gas_regname(index);
  130. {RRX always rotates by 1 bit and does not take an imm}
  131. if shiftmode = SM_RRX then
  132. s:=s+', rrx'
  133. else if shiftmode <> SM_None then
  134. s:=s+', '+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  135. end
  136. else if offset<>0 then
  137. s:=s+', #'+tostr(offset);
  138. case addressmode of
  139. AM_OFFSET:
  140. s:=s+']';
  141. AM_PREINDEXED:
  142. s:=s+']!';
  143. end;
  144. end;
  145. end;
  146. getreferencestring:=s;
  147. end;
  148. function getopstr(const o:toper) : string;
  149. var
  150. hs : string;
  151. first : boolean;
  152. r : tsuperregister;
  153. begin
  154. case o.typ of
  155. top_reg:
  156. getopstr:=gas_regname(o.reg);
  157. top_shifterop:
  158. begin
  159. {RRX is special, it only rotates by 1 and does not take any shiftervalue}
  160. if o.shifterop^.shiftmode=SM_RRX then
  161. getopstr:='rrx'
  162. else if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  163. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  164. else if (o.shifterop^.rs=NR_NO) then
  165. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  166. else internalerror(200308282);
  167. end;
  168. top_const:
  169. getopstr:='#'+tostr(longint(o.val));
  170. top_regset:
  171. begin
  172. getopstr:='{';
  173. first:=true;
  174. for r:=RS_R0 to RS_R15 do
  175. if r in o.regset^ then
  176. begin
  177. if not(first) then
  178. getopstr:=getopstr+',';
  179. getopstr:=getopstr+gas_regname(newreg(o.regtyp,r,o.subreg));
  180. first:=false;
  181. end;
  182. getopstr:=getopstr+'}';
  183. end;
  184. top_conditioncode:
  185. getopstr:=cond2str[o.cc];
  186. top_modeflags:
  187. begin
  188. getopstr:='';
  189. if mfA in o.modeflags then getopstr:=getopstr+'a';
  190. if mfI in o.modeflags then getopstr:=getopstr+'i';
  191. if mfF in o.modeflags then getopstr:=getopstr+'f';
  192. end;
  193. top_ref:
  194. if o.ref^.refaddr=addr_full then
  195. begin
  196. hs:=o.ref^.symbol.name;
  197. if o.ref^.offset>0 then
  198. hs:=hs+'+'+tostr(o.ref^.offset)
  199. else
  200. if o.ref^.offset<0 then
  201. hs:=hs+tostr(o.ref^.offset);
  202. getopstr:=hs;
  203. end
  204. else
  205. getopstr:=getreferencestring(o.ref^);
  206. else
  207. internalerror(2002070604);
  208. end;
  209. end;
  210. Procedure TArmInstrWriter.WriteInstruction(hp : tai);
  211. var op: TAsmOp;
  212. postfix,s: string;
  213. i: byte;
  214. sep: string[3];
  215. begin
  216. op:=taicpu(hp).opcode;
  217. if current_settings.cputype in cpu_thumb2 then
  218. begin
  219. postfix:='';
  220. if taicpu(hp).wideformat then
  221. postfix:='.w';
  222. if taicpu(hp).ops = 0 then
  223. s:=#9+gas_op2str[op]+' '+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix]
  224. else
  225. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix]+postfix+cond2str[taicpu(hp).condition]; // Conditional infixes are deprecated in unified syntax
  226. end
  227. else
  228. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  229. if taicpu(hp).ops<>0 then
  230. begin
  231. sep:=#9;
  232. for i:=0 to taicpu(hp).ops-1 do
  233. begin
  234. // debug code
  235. // writeln(s);
  236. // writeln(taicpu(hp).fileinfo.line);
  237. { LDM and STM use references as first operand but they are written like a register }
  238. if (i=0) and (op in [A_LDM,A_STM,A_FSTM,A_FLDM]) then
  239. begin
  240. case taicpu(hp).oper[0]^.typ of
  241. top_ref:
  242. begin
  243. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  244. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  245. s:=s+'!';
  246. end;
  247. top_reg:
  248. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  249. else
  250. internalerror(200311292);
  251. end;
  252. end
  253. { register count of SFM and LFM is written without # }
  254. else if (i=1) and (op in [A_SFM,A_LFM]) then
  255. begin
  256. case taicpu(hp).oper[1]^.typ of
  257. top_const:
  258. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  259. else
  260. internalerror(200311292);
  261. end;
  262. end
  263. else
  264. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  265. sep:=',';
  266. end;
  267. end;
  268. owner.AsmWriteLn(s);
  269. end;
  270. const
  271. as_arm_gas_info : tasminfo =
  272. (
  273. id : as_gas;
  274. idtxt : 'AS';
  275. asmbin : 'as';
  276. asmcmd : '-o $OBJ $ASM';
  277. supported_targets : [system_arm_linux,system_arm_wince,system_arm_gba,system_arm_palmos,system_arm_nds,system_arm_embedded,system_arm_symbian];
  278. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  279. labelprefix : '.L';
  280. comment : '# ';
  281. dollarsign: '$';
  282. );
  283. as_arm_gas_darwin_info : tasminfo =
  284. (
  285. id : as_darwin;
  286. idtxt : 'AS-Darwin';
  287. asmbin : 'as';
  288. asmcmd : '-o $OBJ $ASM -arch $ARCH';
  289. supported_targets : [system_arm_darwin];
  290. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  291. labelprefix : 'L';
  292. comment : '# ';
  293. dollarsign: '$';
  294. );
  295. begin
  296. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  297. RegisterAssembler(as_arm_gas_darwin_info,TArmAppleGNUAssembler);
  298. end.