cpugas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. or (op=A_LA) or (op=A_BC) or (op=A_JAL)
  201. or (op=A_REM) or (op=A_REMU)
  202. or (op=A_DIV) or (op=A_DIVU)
  203. { A_LI is only a macro if the immediate is not in thez 16-bit range }
  204. or (op=A_LI) or (op=A_AND) or (op=A_XOR);
  205. end;
  206. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  207. var
  208. Op: TAsmOp;
  209. s,s1: string;
  210. i: integer;
  211. tmpfpu: string;
  212. tmpfpu_len: longint;
  213. r: TRegister;
  214. begin
  215. if hp.typ <> ait_instruction then
  216. exit;
  217. op := taicpu(hp).opcode;
  218. case op of
  219. A_P_SET_NOMIPS16:
  220. begin
  221. s := #9 + '.set' + #9 + 'nomips16';
  222. owner.AsmWriteLn(s);
  223. end;
  224. A_P_MASK,
  225. A_P_FMASK:
  226. begin
  227. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  228. owner.AsmWriteLn(s);
  229. end;
  230. A_P_SET_MACRO:
  231. begin
  232. s := #9 + '.set' + #9 + 'macro';
  233. owner.AsmWriteLn(s);
  234. TMIPSGNUAssembler(owner).nomacro:=false;
  235. end;
  236. A_P_SET_REORDER:
  237. begin
  238. s := #9 + '.set' + #9 + 'reorder';
  239. owner.AsmWriteLn(s);
  240. TMIPSGNUAssembler(owner).noreorder:=false;
  241. end;
  242. A_P_SET_NOMACRO:
  243. begin
  244. s := #9 + '.set' + #9 + 'nomacro';
  245. owner.AsmWriteLn(s);
  246. TMIPSGNUAssembler(owner).nomacro:=true;
  247. end;
  248. A_P_SET_NOREORDER:
  249. begin
  250. s := #9 + '.set' + #9 + 'noreorder';
  251. owner.AsmWriteLn(s);
  252. TMIPSGNUAssembler(owner).noreorder:=true;
  253. end;
  254. A_P_SW:
  255. begin
  256. s := #9 + gas_op2str[A_SW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  257. owner.AsmWriteLn(s);
  258. end;
  259. A_P_LW:
  260. begin
  261. s := #9 + gas_op2str[A_LW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  262. owner.AsmWriteLn(s);
  263. end;
  264. A_LDC1:
  265. begin
  266. if (target_info.endian = endian_big) then
  267. begin
  268. s := #9 + gas_op2str[A_LDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  269. + ',' + getopstr(taicpu(hp).oper[1]^);
  270. end
  271. else
  272. begin
  273. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  274. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  275. owner.AsmWriteLn(s);
  276. { bug if $f9/$f19
  277. tmpfpu_len := length(tmpfpu);
  278. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  279. }
  280. r := taicpu(hp).oper[0]^.reg;
  281. setsupreg(r, getsupreg(r) + 1);
  282. tmpfpu := asm_regname(r);
  283. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  284. end;
  285. owner.AsmWriteLn(s);
  286. end;
  287. A_SDC1:
  288. begin
  289. if (target_info.endian = endian_big) then
  290. begin
  291. s := #9 + gas_op2str[A_SDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  292. + ',' + getopstr(taicpu(hp).oper[1]^);
  293. end
  294. else
  295. begin
  296. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  297. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  298. owner.AsmWriteLn(s);
  299. {
  300. tmpfpu_len := length(tmpfpu);
  301. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  302. }
  303. r := taicpu(hp).oper[0]^.reg;
  304. setsupreg(r, getsupreg(r) + 1);
  305. tmpfpu := asm_regname(r);
  306. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  307. end;
  308. owner.AsmWriteLn(s);
  309. end;
  310. else
  311. begin
  312. if is_macro_instruction(op) and TMIPSGNUAssembler(owner).nomacro then
  313. owner.AsmWriteln(#9'.set'#9'macro');
  314. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  315. if taicpu(hp).delayslot_annulled then
  316. s := s + ',a';
  317. if taicpu(hp).ops > 0 then
  318. begin
  319. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  320. for i := 1 to taicpu(hp).ops - 1 do
  321. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  322. end;
  323. owner.AsmWriteLn(s);
  324. if is_macro_instruction(op) and TMIPSGNUAssembler(owner).nomacro then
  325. owner.AsmWriteln(#9'.set'#9'nomacro');
  326. end;
  327. end;
  328. end;
  329. const
  330. as_MIPSEL_as_info: tasminfo =
  331. (
  332. id: as_gas;
  333. idtxt: 'AS';
  334. asmbin: 'as';
  335. asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $ASM';
  336. supported_targets: [system_mipsel_linux];
  337. flags: [ af_needar, af_smartlink_sections];
  338. labelprefix: '.L';
  339. comment: '# ';
  340. dollarsign: '$';
  341. );
  342. as_MIPSEB_as_info: tasminfo =
  343. (
  344. id: as_gas;
  345. idtxt: 'AS';
  346. asmbin: 'as';
  347. asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $ASM';
  348. supported_targets: [system_mipseb_linux];
  349. flags: [ af_needar, af_smartlink_sections];
  350. labelprefix: '.L';
  351. comment: '# ';
  352. dollarsign: '$';
  353. );
  354. begin
  355. {$ifdef MIPSEL}
  356. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  357. {$else MIPSEL}
  358. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  359. {$endif MIPSEL}
  360. end.