cpugas.pas 13 KB

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