agarmgas.pas 14 KB

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