agarmgas.pas 17 KB

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