2
0

agarmgas.pas 16 KB

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