agarmgas.pas 15 KB

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