agarmgas.pas 17 KB

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