agarmgas.pas 14 KB

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