agarmgas.pas 18 KB

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