2
0

agarmgas.pas 16 KB

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