cpugas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. {
  2. Copyright (c) 1999-2009 by Florian Klaempfl and David Zhang
  3. This unit implements an asmoutput class for MIPS assembly syntax
  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. unit cpugas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase, aasmbase, globtype,
  22. aasmtai, aasmcpu, assemble, aggas;
  23. type
  24. TMIPSGNUAssembler = class(TGNUassembler)
  25. nomacro, noreorder, noat : boolean;
  26. constructor create(smart: boolean); override;
  27. {# Constructs the command line for calling the assembler }
  28. function MakeCmdLine: TCmdStr; override;
  29. end;
  30. TMIPSInstrWriter = class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. const
  34. use_std_regnames : boolean =
  35. {$ifndef USE_MIPS_GAS_REGS}
  36. true;
  37. {$else}
  38. false;
  39. {$endif}
  40. implementation
  41. uses
  42. cutils, systems, cpuinfo,
  43. verbose, itcpugas, cgbase, cgutils;
  44. function gas_std_regname(r:Tregister):string;
  45. var
  46. hr: tregister;
  47. begin
  48. { Double uses the same table as single }
  49. hr := r;
  50. case getsubreg(hr) of
  51. R_SUBFD:
  52. setsubreg(hr, R_SUBFS);
  53. R_SUBL, R_SUBW, R_SUBD, R_SUBQ:
  54. setsubreg(hr, R_SUBD);
  55. end;
  56. result:=std_regname(hr);
  57. end;
  58. function asm_regname(reg : TRegister) : string;
  59. begin
  60. if use_std_regnames then
  61. asm_regname:='$'+gas_std_regname(reg)
  62. else
  63. asm_regname:=gas_regname(reg);
  64. end;
  65. {****************************************************************************}
  66. { GNU MIPS Assembler writer }
  67. {****************************************************************************}
  68. constructor TMIPSGNUAssembler.create(smart: boolean);
  69. begin
  70. inherited create(smart);
  71. InstrWriter := TMIPSInstrWriter.create(self);
  72. nomacro:=false;
  73. noreorder:=false;
  74. noat:=false;
  75. end;
  76. function TMIPSGNUAssembler.MakeCmdLine: TCmdStr;
  77. begin
  78. result := Inherited MakeCmdLine;
  79. { ABI selection }
  80. Replace(result,'$ABI','-mabi='+abitypestr[mips_abi]);
  81. { ARCH selection }
  82. Replace(result,'$ARCH','-march='+lower(cputypestr[mips_cpu]));
  83. end;
  84. {****************************************************************************}
  85. { Helper routines for Instruction Writer }
  86. {****************************************************************************}
  87. function GetReferenceString(var ref: TReference): string;
  88. var
  89. reg: TRegister;
  90. regstr: string;
  91. begin
  92. result:='';
  93. if assigned(ref.symbol) then
  94. result:=ref.symbol.name;
  95. if (ref.offset<0) then
  96. result:=result+tostr(ref.offset)
  97. else if (ref.offset>0) then
  98. begin
  99. if assigned(ref.symbol) then
  100. result:=result+'+';
  101. result:=result+tostr(ref.offset);
  102. end;
  103. { either base or index may be present, but not both }
  104. reg:=ref.base;
  105. if (reg=NR_NO) then
  106. reg:=ref.index
  107. else if (ref.index<>NR_NO) then
  108. InternalError(2013013001);
  109. if (reg=NR_NO) then
  110. regstr:=''
  111. else
  112. regstr:='('+asm_regname(reg)+')';
  113. case ref.refaddr of
  114. addr_no,
  115. addr_full:
  116. if assigned(ref.symbol) and (reg<>NR_NO) then
  117. InternalError(2013013002)
  118. else
  119. begin
  120. result:=result+regstr;
  121. exit;
  122. end;
  123. addr_pic:
  124. result:='%got('+result;
  125. addr_high:
  126. result:='%hi('+result;
  127. addr_low:
  128. result:='%lo('+result;
  129. addr_pic_call16:
  130. result:='%call16('+result;
  131. addr_low_pic:
  132. result:='%got_lo('+result;
  133. addr_high_pic:
  134. result:='%got_hi('+result;
  135. addr_low_call:
  136. result:='%call_lo('+result;
  137. addr_high_call:
  138. result:='%call_hi('+result;
  139. else
  140. InternalError(2013013003);
  141. end;
  142. result:=result+')'+regstr;
  143. end;
  144. function getopstr(const Oper: TOper): string;
  145. begin
  146. with Oper do
  147. case typ of
  148. top_reg:
  149. getopstr := asm_regname(reg);
  150. top_const:
  151. getopstr := tostr(longint(val));
  152. top_ref:
  153. getopstr := getreferencestring(ref^);
  154. else
  155. internalerror(10001);
  156. end;
  157. end;
  158. function getopstr_4(const Oper: TOper): string;
  159. var
  160. tmpref: treference;
  161. begin
  162. with Oper do
  163. case typ of
  164. top_ref:
  165. begin
  166. tmpref := ref^;
  167. Inc(tmpref.offset, 4);
  168. getopstr_4 := getreferencestring(tmpref);
  169. end;
  170. else
  171. internalerror(2007050403);
  172. end;
  173. end;
  174. {
  175. function getnextfpreg(tmpfpu : shortstring) : shortstring;
  176. begin
  177. case length(tmpfpu) of
  178. 3:
  179. if (tmpfpu[3] = '9') then
  180. tmpfpu:='$f10'
  181. else
  182. tmpfpu[3] := succ(tmpfpu[3]);
  183. 4:
  184. if (tmpfpu[4] = '9') then
  185. tmpfpu:='$f20'
  186. else
  187. tmpfpu[4] := succ(tmpfpu[4]);
  188. else
  189. internalerror(20120531);
  190. end;
  191. getnextfpreg := tmpfpu;
  192. end;
  193. }
  194. function is_macro_instruction(op : TAsmOp) : boolean;
  195. begin
  196. is_macro_instruction :=
  197. { 'seq', 'sge', 'sgeu', 'sgt', 'sgtu', 'sle', 'sleu', 'sne', }
  198. (op=A_SEQ) or (op = A_SGE) or (op=A_SGEU) or (op=A_SGT) or
  199. (op=A_SGTU) or (op=A_SLE) or (op=A_SLEU) or (op=A_SNE)
  200. { JAL is not here! See comments in TCGMIPS.a_call_name. }
  201. or (op=A_LA) or (op=A_BC) {or (op=A_JAL)}
  202. or (op=A_REM) or (op=A_REMU)
  203. or (op=A_DIV) or (op=A_DIVU)
  204. { A_LI is only a macro if the immediate is not in thez 16-bit range }
  205. or (op=A_LI) or (op=A_AND) or (op=A_XOR);
  206. end;
  207. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  208. var
  209. Op: TAsmOp;
  210. s,s1: string;
  211. i: integer;
  212. tmpfpu: string;
  213. tmpfpu_len: longint;
  214. r: TRegister;
  215. begin
  216. if hp.typ <> ait_instruction then
  217. exit;
  218. op := taicpu(hp).opcode;
  219. case op of
  220. A_P_SET_NOMIPS16:
  221. begin
  222. s := #9 + '.set' + #9 + 'nomips16';
  223. owner.AsmWriteLn(s);
  224. end;
  225. A_P_MASK,
  226. A_P_FMASK:
  227. begin
  228. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  229. owner.AsmWriteLn(s);
  230. end;
  231. A_P_SET_MACRO:
  232. begin
  233. s := #9 + '.set' + #9 + 'macro';
  234. owner.AsmWriteLn(s);
  235. TMIPSGNUAssembler(owner).nomacro:=false;
  236. end;
  237. A_P_SET_REORDER:
  238. begin
  239. s := #9 + '.set' + #9 + 'reorder';
  240. owner.AsmWriteLn(s);
  241. TMIPSGNUAssembler(owner).noreorder:=false;
  242. end;
  243. A_P_SET_NOMACRO:
  244. begin
  245. s := #9 + '.set' + #9 + 'nomacro';
  246. owner.AsmWriteLn(s);
  247. TMIPSGNUAssembler(owner).nomacro:=true;
  248. end;
  249. A_P_SET_NOREORDER:
  250. begin
  251. s := #9 + '.set' + #9 + 'noreorder';
  252. owner.AsmWriteLn(s);
  253. TMIPSGNUAssembler(owner).noreorder:=true;
  254. end;
  255. A_P_SW:
  256. begin
  257. s := #9 + gas_op2str[A_SW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  258. owner.AsmWriteLn(s);
  259. end;
  260. A_P_LW:
  261. begin
  262. s := #9 + gas_op2str[A_LW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  263. owner.AsmWriteLn(s);
  264. end;
  265. A_LDC1:
  266. begin
  267. if (target_info.endian = endian_big) then
  268. begin
  269. s := #9 + gas_op2str[A_LDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  270. + ',' + getopstr(taicpu(hp).oper[1]^);
  271. end
  272. else
  273. begin
  274. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  275. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  276. owner.AsmWriteLn(s);
  277. { bug if $f9/$f19
  278. tmpfpu_len := length(tmpfpu);
  279. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  280. }
  281. r := taicpu(hp).oper[0]^.reg;
  282. setsupreg(r, getsupreg(r) + 1);
  283. tmpfpu := asm_regname(r);
  284. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  285. end;
  286. owner.AsmWriteLn(s);
  287. end;
  288. A_SDC1:
  289. begin
  290. if (target_info.endian = endian_big) then
  291. begin
  292. s := #9 + gas_op2str[A_SDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  293. + ',' + getopstr(taicpu(hp).oper[1]^);
  294. end
  295. else
  296. begin
  297. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  298. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  299. owner.AsmWriteLn(s);
  300. {
  301. tmpfpu_len := length(tmpfpu);
  302. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  303. }
  304. r := taicpu(hp).oper[0]^.reg;
  305. setsupreg(r, getsupreg(r) + 1);
  306. tmpfpu := asm_regname(r);
  307. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  308. end;
  309. owner.AsmWriteLn(s);
  310. end;
  311. else
  312. begin
  313. if is_macro_instruction(op) and TMIPSGNUAssembler(owner).nomacro then
  314. owner.AsmWriteln(#9'.set'#9'macro');
  315. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  316. if taicpu(hp).delayslot_annulled then
  317. s := s + ',a';
  318. if taicpu(hp).ops > 0 then
  319. begin
  320. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  321. for i := 1 to taicpu(hp).ops - 1 do
  322. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  323. end;
  324. owner.AsmWriteLn(s);
  325. if is_macro_instruction(op) and TMIPSGNUAssembler(owner).nomacro then
  326. owner.AsmWriteln(#9'.set'#9'nomacro');
  327. end;
  328. end;
  329. end;
  330. const
  331. as_MIPSEL_as_info: tasminfo =
  332. (
  333. id: as_gas;
  334. idtxt: 'AS';
  335. asmbin: 'as';
  336. asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $ASM';
  337. supported_targets: [system_mipsel_linux];
  338. flags: [ af_needar, af_smartlink_sections];
  339. labelprefix: '.L';
  340. comment: '# ';
  341. dollarsign: '$';
  342. );
  343. as_MIPSEB_as_info: tasminfo =
  344. (
  345. id: as_gas;
  346. idtxt: 'AS';
  347. asmbin: 'as';
  348. asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $ASM';
  349. supported_targets: [system_mipseb_linux];
  350. flags: [ af_needar, af_smartlink_sections];
  351. labelprefix: '.L';
  352. comment: '# ';
  353. dollarsign: '$';
  354. );
  355. begin
  356. {$ifdef MIPSEL}
  357. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  358. {$else MIPSEL}
  359. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  360. {$endif MIPSEL}
  361. end.