agarmgas.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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,systems,
  24. aasmtai,
  25. aggas,
  26. cpubase,cpuinfo;
  27. type
  28. TARMGNUAssembler=class(TGNUassembler)
  29. constructor create(info: pasminfo; 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(info: pasminfo; smart: boolean); override;
  38. end;
  39. const
  40. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  41. '','lsl','lsr','asr','ror','rrx');
  42. const
  43. cputype_to_gas_march : array[tcputype] of string = (
  44. '', // cpu_none
  45. 'armv3',
  46. 'armv4',
  47. 'armv4t',
  48. 'armv5',
  49. 'armv5t',
  50. 'armv5te',
  51. 'armv5tej',
  52. 'armv6',
  53. 'armv6k',
  54. 'armv6t2',
  55. 'armv6z',
  56. 'armv6-m',
  57. 'armv7',
  58. 'armv7-a',
  59. 'armv7-r',
  60. 'armv7-m',
  61. 'armv7e-m');
  62. implementation
  63. uses
  64. cutils,globals,verbose,
  65. assemble,
  66. aasmcpu,
  67. itcpugas,
  68. cgbase,cgutils;
  69. {****************************************************************************}
  70. { GNU Arm Assembler writer }
  71. {****************************************************************************}
  72. constructor TArmGNUAssembler.create(info: pasminfo; smart: boolean);
  73. begin
  74. inherited;
  75. InstrWriter := TArmInstrWriter.create(self);
  76. end;
  77. function TArmGNUAssembler.MakeCmdLine: TCmdStr;
  78. begin
  79. result:=inherited MakeCmdLine;
  80. if (current_settings.fputype = fpu_soft) then
  81. result:='-mfpu=softvfp '+result;
  82. if (current_settings.fputype = fpu_vfpv2) then
  83. result:='-mfpu=vfpv2 '+result;
  84. if (current_settings.fputype = fpu_vfpv3) then
  85. result:='-mfpu=vfpv3 '+result;
  86. if (current_settings.fputype = fpu_vfpv3_d16) then
  87. result:='-mfpu=vfpv3-d16 '+result;
  88. if (current_settings.fputype = fpu_fpv4_s16) then
  89. result:='-mfpu=fpv4-sp-d16 '+result;
  90. if (current_settings.fputype = fpu_vfpv4) then
  91. result:='-mfpu=vfpv4 '+result;
  92. if GenerateThumb2Code then
  93. result:='-march='+cputype_to_gas_march[current_settings.cputype]+' -mthumb -mthumb-interwork '+result
  94. else if GenerateThumbCode then
  95. result:='-march='+cputype_to_gas_march[current_settings.cputype]+' -mthumb -mthumb-interwork '+result
  96. else
  97. result:='-march='+cputype_to_gas_march[current_settings.cputype]+' '+result;
  98. if target_info.abi = abi_eabihf then
  99. { options based on what gcc uses on debian armhf }
  100. result:='-mfloat-abi=hard -meabi=5 '+result;
  101. end;
  102. procedure TArmGNUAssembler.WriteExtraHeader;
  103. begin
  104. inherited WriteExtraHeader;
  105. if GenerateThumb2Code then
  106. writer.AsmWriteLn(#9'.syntax unified');
  107. end;
  108. {****************************************************************************}
  109. { GNU/Apple ARM Assembler writer }
  110. {****************************************************************************}
  111. constructor TArmAppleGNUAssembler.create(info: pasminfo; smart: boolean);
  112. begin
  113. inherited;
  114. InstrWriter := TArmInstrWriter.create(self);
  115. end;
  116. {****************************************************************************}
  117. { Helper routines for Instruction Writer }
  118. {****************************************************************************}
  119. function getreferencestring(var ref : treference) : string;
  120. var
  121. s : string;
  122. begin
  123. with ref do
  124. begin
  125. {$ifdef extdebug}
  126. // if base=NR_NO then
  127. // internalerror(200308292);
  128. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  129. // internalerror(200308293);
  130. {$endif extdebug}
  131. if assigned(symbol) then
  132. begin
  133. if (base<>NR_NO) and not(is_pc(base)) then
  134. internalerror(200309011);
  135. s:=symbol.name;
  136. if offset<>0 then
  137. s:=s+tostr_with_plus(offset);
  138. if refaddr=addr_pic then
  139. s:=s+'(PLT)';
  140. end
  141. else
  142. begin
  143. s:='['+gas_regname(base);
  144. if addressmode=AM_POSTINDEXED then
  145. s:=s+']';
  146. if index<>NR_NO then
  147. begin
  148. if signindex<0 then
  149. s:=s+', -'
  150. else
  151. s:=s+', ';
  152. s:=s+gas_regname(index);
  153. {RRX always rotates by 1 bit and does not take an imm}
  154. if shiftmode = SM_RRX then
  155. s:=s+', rrx'
  156. else if shiftmode <> SM_None then
  157. s:=s+', '+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  158. end
  159. else if offset<>0 then
  160. s:=s+', #'+tostr(offset);
  161. case addressmode of
  162. AM_OFFSET:
  163. s:=s+']';
  164. AM_PREINDEXED:
  165. s:=s+']!';
  166. end;
  167. end;
  168. end;
  169. getreferencestring:=s;
  170. end;
  171. function getopstr(const o:toper) : string;
  172. var
  173. hs : string;
  174. first : boolean;
  175. r, rs : tsuperregister;
  176. begin
  177. case o.typ of
  178. top_reg:
  179. getopstr:=gas_regname(o.reg);
  180. top_shifterop:
  181. begin
  182. {RRX is special, it only rotates by 1 and does not take any shiftervalue}
  183. if o.shifterop^.shiftmode=SM_RRX then
  184. getopstr:='rrx'
  185. else if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  186. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  187. else if (o.shifterop^.rs=NR_NO) then
  188. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  189. else internalerror(200308282);
  190. end;
  191. top_const:
  192. getopstr:='#'+tostr(longint(o.val));
  193. top_regset:
  194. begin
  195. getopstr:='{';
  196. first:=true;
  197. if R_SUBFS=o.subreg then
  198. begin
  199. for r:=0 to 31 do // S0 to S31
  200. if r in o.regset^ then
  201. begin
  202. if not(first) then
  203. getopstr:=getopstr+',';
  204. if odd(r) then
  205. rs:=(r shr 1)+RS_S1
  206. else
  207. rs:=(r shr 1)+RS_S0;
  208. getopstr:=getopstr+gas_regname(newreg(o.regtyp,rs,o.subreg));
  209. first:=false;
  210. end;
  211. end
  212. else if R_SUBFD=o.subreg then
  213. begin
  214. for r:=0 to 31 do
  215. if r in o.regset^ then
  216. begin
  217. if not(first) then
  218. getopstr:=getopstr+',';
  219. rs:=r+RS_D0;
  220. getopstr:=getopstr+gas_regname(newreg(o.regtyp,rs,o.subreg));
  221. first:=false;
  222. end;
  223. end
  224. else
  225. begin
  226. for r:=RS_R0 to RS_R15 do
  227. if r in o.regset^ then
  228. begin
  229. if not(first) then
  230. getopstr:=getopstr+',';
  231. getopstr:=getopstr+gas_regname(newreg(o.regtyp,r,o.subreg));
  232. first:=false;
  233. end;
  234. end;
  235. getopstr:=getopstr+'}';
  236. if o.usermode then
  237. getopstr:=getopstr+'^';
  238. end;
  239. top_conditioncode:
  240. getopstr:=cond2str[o.cc];
  241. top_modeflags:
  242. begin
  243. getopstr:='';
  244. if mfA in o.modeflags then getopstr:=getopstr+'a';
  245. if mfI in o.modeflags then getopstr:=getopstr+'i';
  246. if mfF in o.modeflags then getopstr:=getopstr+'f';
  247. end;
  248. top_ref:
  249. if o.ref^.refaddr=addr_full then
  250. begin
  251. hs:=o.ref^.symbol.name;
  252. if o.ref^.offset>0 then
  253. hs:=hs+'+'+tostr(o.ref^.offset)
  254. else
  255. if o.ref^.offset<0 then
  256. hs:=hs+tostr(o.ref^.offset);
  257. getopstr:=hs;
  258. end
  259. else
  260. getopstr:=getreferencestring(o.ref^);
  261. top_specialreg:
  262. begin
  263. getopstr:=gas_regname(o.specialreg);
  264. if o.specialflags<>[] then
  265. begin
  266. getopstr:=getopstr+'_';
  267. if srC in o.specialflags then getopstr:=getopstr+'c';
  268. if srX in o.specialflags then getopstr:=getopstr+'x';
  269. if srF in o.specialflags then getopstr:=getopstr+'f';
  270. if srS in o.specialflags then getopstr:=getopstr+'s';
  271. end;
  272. end
  273. else
  274. internalerror(2002070604);
  275. end;
  276. end;
  277. Procedure TArmInstrWriter.WriteInstruction(hp : tai);
  278. var op: TAsmOp;
  279. postfix,s: string;
  280. i: byte;
  281. sep: string[3];
  282. begin
  283. op:=taicpu(hp).opcode;
  284. if GenerateThumb2Code then
  285. begin
  286. postfix:='';
  287. if taicpu(hp).wideformat then
  288. postfix:='.w';
  289. if taicpu(hp).ops = 0 then
  290. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix]
  291. else if taicpu(hp).oppostfix in [PF_8..PF_U32F64] then
  292. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix]
  293. else
  294. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix]+cond2str[taicpu(hp).condition]+postfix; // Conditional infixes are deprecated in unified syntax
  295. end
  296. else
  297. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  298. if taicpu(hp).ops<>0 then
  299. begin
  300. sep:=#9;
  301. for i:=0 to taicpu(hp).ops-1 do
  302. begin
  303. // debug code
  304. // writeln(s);
  305. // writeln(taicpu(hp).fileinfo.line);
  306. { LDM and STM use references as first operand but they are written like a register }
  307. if (i=0) and (op in [A_LDM,A_STM,A_FSTM,A_FLDM,A_VSTM,A_VLDM]) then
  308. begin
  309. case taicpu(hp).oper[0]^.typ of
  310. top_ref:
  311. begin
  312. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  313. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  314. s:=s+'!';
  315. end;
  316. top_reg:
  317. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  318. else
  319. internalerror(200311292);
  320. end;
  321. end
  322. { register count of SFM and LFM is written without # }
  323. else if (i=1) and (op in [A_SFM,A_LFM]) then
  324. begin
  325. case taicpu(hp).oper[1]^.typ of
  326. top_const:
  327. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  328. else
  329. internalerror(200311292);
  330. end;
  331. end
  332. else
  333. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  334. sep:=',';
  335. end;
  336. end;
  337. owner.writer.AsmWriteLn(s);
  338. end;
  339. const
  340. as_arm_gas_info : tasminfo =
  341. (
  342. id : as_gas;
  343. idtxt : 'AS';
  344. asmbin : 'as';
  345. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  346. supported_targets : [system_arm_linux,system_arm_wince,system_arm_gba,system_arm_palmos,system_arm_nds,
  347. system_arm_embedded,system_arm_symbian,system_arm_android];
  348. flags : [af_needar,af_smartlink_sections];
  349. labelprefix : '.L';
  350. comment : '# ';
  351. dollarsign: '$';
  352. );
  353. as_arm_gas_darwin_info : tasminfo =
  354. (
  355. id : as_darwin;
  356. idtxt : 'AS-Darwin';
  357. asmbin : 'as';
  358. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch $ARCH';
  359. supported_targets : [system_arm_darwin];
  360. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  361. labelprefix : 'L';
  362. comment : '# ';
  363. dollarsign: '$';
  364. );
  365. begin
  366. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  367. RegisterAssembler(as_arm_gas_darwin_info,TArmAppleGNUAssembler);
  368. end.