cpugas.pas 13 KB

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