agarmgas.pas 16 KB

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