agarmgas.pas 12 KB

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