cpugas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. globals, 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. if getregtype(r)=R_SPECIALREGISTER then
  57. result:=tostr(getsupreg(r))
  58. else
  59. result:=std_regname(hr);
  60. end;
  61. function asm_regname(reg : TRegister) : string;
  62. begin
  63. if use_std_regnames then
  64. asm_regname:='$'+gas_std_regname(reg)
  65. else
  66. asm_regname:=gas_regname(reg);
  67. end;
  68. {****************************************************************************}
  69. { GNU MIPS Assembler writer }
  70. {****************************************************************************}
  71. constructor TMIPSGNUAssembler.create(smart: boolean);
  72. begin
  73. inherited create(smart);
  74. InstrWriter := TMIPSInstrWriter.create(self);
  75. nomacro:=false;
  76. noreorder:=false;
  77. noat:=false;
  78. end;
  79. function TMIPSGNUAssembler.MakeCmdLine: TCmdStr;
  80. begin
  81. result := Inherited MakeCmdLine;
  82. { ABI selection }
  83. Replace(result,'$ABI','-mabi='+abitypestr[mips_abi]);
  84. { ARCH selection }
  85. Replace(result,'$ARCH','-march='+lower(cputypestr[current_settings.cputype]));
  86. end;
  87. {****************************************************************************}
  88. { Helper routines for Instruction Writer }
  89. {****************************************************************************}
  90. function GetReferenceString(var ref: TReference): string;
  91. var
  92. reg: TRegister;
  93. regstr: string;
  94. begin
  95. result:='';
  96. if assigned(ref.symbol) then
  97. result:=ref.symbol.name;
  98. if (ref.offset<0) then
  99. result:=result+tostr(ref.offset)
  100. else if (ref.offset>0) then
  101. begin
  102. if assigned(ref.symbol) then
  103. result:=result+'+';
  104. result:=result+tostr(ref.offset);
  105. end
  106. { asmreader appears to treat literal numbers as references }
  107. else if (ref.symbol=nil) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  108. result:='0';
  109. { either base or index may be present, but not both }
  110. reg:=ref.base;
  111. if (reg=NR_NO) then
  112. reg:=ref.index
  113. else if (ref.index<>NR_NO) then
  114. InternalError(2013013001);
  115. if (reg=NR_NO) then
  116. regstr:=''
  117. else
  118. regstr:='('+asm_regname(reg)+')';
  119. case ref.refaddr of
  120. addr_no,
  121. addr_full:
  122. if assigned(ref.symbol) and (reg<>NR_NO) then
  123. InternalError(2013013002)
  124. else
  125. begin
  126. result:=result+regstr;
  127. exit;
  128. end;
  129. addr_pic:
  130. result:='%got('+result;
  131. addr_high:
  132. result:='%hi('+result;
  133. addr_low:
  134. result:='%lo('+result;
  135. addr_pic_call16:
  136. result:='%call16('+result;
  137. addr_low_pic:
  138. result:='%got_lo('+result;
  139. addr_high_pic:
  140. result:='%got_hi('+result;
  141. addr_low_call:
  142. result:='%call_lo('+result;
  143. addr_high_call:
  144. result:='%call_hi('+result;
  145. else
  146. InternalError(2013013003);
  147. end;
  148. result:=result+')'+regstr;
  149. end;
  150. function getopstr(const Oper: TOper): string;
  151. begin
  152. with Oper do
  153. case typ of
  154. top_reg:
  155. getopstr := asm_regname(reg);
  156. top_const:
  157. getopstr := tostr(longint(val));
  158. top_ref:
  159. getopstr := getreferencestring(ref^);
  160. else
  161. internalerror(10001);
  162. end;
  163. end;
  164. function getopstr_4(const Oper: TOper): string;
  165. var
  166. tmpref: treference;
  167. begin
  168. with Oper do
  169. case typ of
  170. top_ref:
  171. begin
  172. tmpref := ref^;
  173. Inc(tmpref.offset, 4);
  174. getopstr_4 := getreferencestring(tmpref);
  175. end;
  176. else
  177. internalerror(2007050403);
  178. end;
  179. end;
  180. {
  181. function getnextfpreg(tmpfpu : shortstring) : shortstring;
  182. begin
  183. case length(tmpfpu) of
  184. 3:
  185. if (tmpfpu[3] = '9') then
  186. tmpfpu:='$f10'
  187. else
  188. tmpfpu[3] := succ(tmpfpu[3]);
  189. 4:
  190. if (tmpfpu[4] = '9') then
  191. tmpfpu:='$f20'
  192. else
  193. tmpfpu[4] := succ(tmpfpu[4]);
  194. else
  195. internalerror(20120531);
  196. end;
  197. getnextfpreg := tmpfpu;
  198. end;
  199. }
  200. function is_macro_instruction(ai : taicpu) : boolean;
  201. var
  202. op: tasmop;
  203. begin
  204. op:=ai.opcode;
  205. is_macro_instruction :=
  206. { 'seq', 'sge', 'sgeu', 'sgt', 'sgtu', 'sle', 'sleu', 'sne', }
  207. (op=A_SEQ) or (op = A_SGE) or (op=A_SGEU) or (op=A_SGT) or
  208. (op=A_SGTU) or (op=A_SLE) or (op=A_SLEU) or (op=A_SNE)
  209. { JAL is not here! See comments in TCGMIPS.a_call_name. }
  210. or (op=A_LA) or ((op=A_BC) and
  211. not (ai.condition in [C_EQ,C_NE,C_GTZ,C_GEZ,C_LTZ,C_LEZ,C_COP1TRUE,C_COP1FALSE])) {or (op=A_JAL)}
  212. or (op=A_REM) or (op=A_REMU)
  213. { DIV and DIVU are normally macros, but use $zero as first arg to generate a CPU instruction. }
  214. or ((op=A_DIV) or (op=A_DIVU) and
  215. ((ai.ops<>3) or (ai.oper[0]^.typ<>top_reg) or (ai.oper[0]^.reg<>NR_R0)))
  216. or (op=A_MULO) or (op=A_MULOU)
  217. { A_LI is only a macro if the immediate is not in thez 16-bit range }
  218. or (op=A_LI);
  219. end;
  220. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  221. var
  222. Op: TAsmOp;
  223. s,s1: string;
  224. i: integer;
  225. tmpfpu: string;
  226. tmpfpu_len: longint;
  227. r: TRegister;
  228. begin
  229. if hp.typ <> ait_instruction then
  230. exit;
  231. op := taicpu(hp).opcode;
  232. case op of
  233. A_P_SET_NOMIPS16:
  234. begin
  235. owner.AsmWriteLn(#9'.set'#9'nomips16');
  236. end;
  237. A_P_MASK,
  238. A_P_FMASK:
  239. begin
  240. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  241. owner.AsmWriteLn(s);
  242. end;
  243. A_P_SET_MACRO:
  244. begin
  245. owner.AsmWriteLn(#9'.set'#9'macro');
  246. TMIPSGNUAssembler(owner).nomacro:=false;
  247. end;
  248. A_P_SET_REORDER:
  249. begin
  250. owner.AsmWriteLn(#9'.set'#9'reorder');
  251. TMIPSGNUAssembler(owner).noreorder:=false;
  252. end;
  253. A_P_SET_NOMACRO:
  254. begin
  255. owner.AsmWriteLn(#9'.set'#9'nomacro');
  256. TMIPSGNUAssembler(owner).nomacro:=true;
  257. end;
  258. A_P_SET_NOREORDER:
  259. begin
  260. owner.AsmWriteLn(#9'.set'#9'noreorder');
  261. TMIPSGNUAssembler(owner).noreorder:=true;
  262. end;
  263. A_P_SET_NOAT:
  264. begin
  265. owner.AsmWriteln(#9'.set'#9'noat');
  266. TMIPSGNUAssembler(owner).noat:=true;
  267. end;
  268. A_P_SET_AT:
  269. begin
  270. owner.AsmWriteln(#9'.set'#9'at');
  271. TMIPSGNUAssembler(owner).noat:=false;
  272. end;
  273. A_LDC1:
  274. begin
  275. if (target_info.endian = endian_big) then
  276. begin
  277. s := #9 + gas_op2str[A_LDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  278. + ',' + getopstr(taicpu(hp).oper[1]^);
  279. end
  280. else
  281. begin
  282. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  283. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  284. owner.AsmWriteLn(s);
  285. { bug if $f9/$f19
  286. tmpfpu_len := length(tmpfpu);
  287. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  288. }
  289. r := taicpu(hp).oper[0]^.reg;
  290. setsupreg(r, getsupreg(r) + 1);
  291. tmpfpu := asm_regname(r);
  292. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  293. end;
  294. owner.AsmWriteLn(s);
  295. end;
  296. A_SDC1:
  297. begin
  298. if (target_info.endian = endian_big) then
  299. begin
  300. s := #9 + gas_op2str[A_SDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  301. + ',' + getopstr(taicpu(hp).oper[1]^);
  302. end
  303. else
  304. begin
  305. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  306. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  307. owner.AsmWriteLn(s);
  308. {
  309. tmpfpu_len := length(tmpfpu);
  310. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  311. }
  312. r := taicpu(hp).oper[0]^.reg;
  313. setsupreg(r, getsupreg(r) + 1);
  314. tmpfpu := asm_regname(r);
  315. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  316. end;
  317. owner.AsmWriteLn(s);
  318. end;
  319. else
  320. begin
  321. if is_macro_instruction(taicpu(hp)) and TMIPSGNUAssembler(owner).nomacro then
  322. owner.AsmWriteln(#9'.set'#9'macro');
  323. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  324. if taicpu(hp).delayslot_annulled then
  325. s := s + ',a';
  326. if taicpu(hp).ops > 0 then
  327. begin
  328. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  329. for i := 1 to taicpu(hp).ops - 1 do
  330. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  331. end;
  332. owner.AsmWriteLn(s);
  333. if is_macro_instruction(taicpu(hp)) and TMIPSGNUAssembler(owner).nomacro then
  334. owner.AsmWriteln(#9'.set'#9'nomacro');
  335. end;
  336. end;
  337. end;
  338. const
  339. as_MIPSEL_as_info: tasminfo =
  340. (
  341. id: as_gas;
  342. idtxt: 'AS';
  343. asmbin: 'as';
  344. asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $EXTRAOPT $ASM';
  345. supported_targets: [system_mipsel_linux];
  346. flags: [ af_needar, af_smartlink_sections];
  347. labelprefix: '.L';
  348. comment: '# ';
  349. dollarsign: '$';
  350. );
  351. as_MIPSEB_as_info: tasminfo =
  352. (
  353. id: as_gas;
  354. idtxt: 'AS';
  355. asmbin: 'as';
  356. asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $EXTRAOPT $ASM';
  357. supported_targets: [system_mipseb_linux];
  358. flags: [ af_needar, af_smartlink_sections];
  359. labelprefix: '.L';
  360. comment: '# ';
  361. dollarsign: '$';
  362. );
  363. begin
  364. {$ifdef MIPSEL}
  365. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  366. {$else MIPSEL}
  367. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  368. {$endif MIPSEL}
  369. end.