agarmgas.pas 17 KB

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