cpugas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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,
  22. aasmtai, aasmcpu, assemble, aggas;
  23. type
  24. TMIPSGNUAssembler = class(TGNUassembler)
  25. nomacro, noreorder, noat : boolean;
  26. constructor create(smart: boolean); override;
  27. end;
  28. TMIPSInstrWriter = class(TCPUInstrWriter)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. const
  32. use_std_regnames : boolean =
  33. {$ifndef USE_MIPS_GAS_REGS}
  34. true;
  35. {$else}
  36. false;
  37. {$endif}
  38. implementation
  39. uses
  40. aasmbase, cutils, systems,
  41. verbose, itcpugas, cgbase, cgutils;
  42. function gas_std_regname(r:Tregister):string;
  43. var
  44. hr: tregister;
  45. p: longint;
  46. begin
  47. { Double uses the same table as single }
  48. hr := r;
  49. case getsubreg(hr) of
  50. R_SUBFD:
  51. setsubreg(hr, R_SUBFS);
  52. R_SUBL, R_SUBW, R_SUBD, R_SUBQ:
  53. setsubreg(hr, R_SUBD);
  54. end;
  55. result:=std_regname(hr);
  56. end;
  57. function asm_regname(reg : TRegister) : string;
  58. begin
  59. if use_std_regnames then
  60. asm_regname:='$'+gas_std_regname(reg)
  61. else
  62. asm_regname:=gas_regname(reg);
  63. end;
  64. {****************************************************************************}
  65. { GNU MIPS Assembler writer }
  66. {****************************************************************************}
  67. constructor TMIPSGNUAssembler.create(smart: boolean);
  68. begin
  69. inherited create(smart);
  70. InstrWriter := TMIPSInstrWriter.create(self);
  71. nomacro:=false;
  72. noreorder:=false;
  73. noat:=false;
  74. end;
  75. {****************************************************************************}
  76. { Helper routines for Instruction Writer }
  77. {****************************************************************************}
  78. function GetReferenceString(var ref: TReference): string;
  79. var
  80. hasgot : boolean;
  81. gotprefix : string;
  82. begin
  83. GetReferenceString := '';
  84. hasgot:=false;
  85. with ref do
  86. begin
  87. if (base = NR_NO) and (index = NR_NO) then
  88. begin
  89. if assigned(symbol) then
  90. begin
  91. GetReferenceString := symbol.Name;
  92. if symbol.typ=AT_FUNCTION then
  93. gotprefix:='%call16('
  94. else
  95. gotprefix:='%got(';
  96. hasgot:=true;
  97. end;
  98. if offset > 0 then
  99. GetReferenceString := GetReferenceString + '+' + ToStr(offset)
  100. else if offset < 0 then
  101. GetReferenceString := GetReferenceString + ToStr(offset);
  102. case refaddr of
  103. addr_high:
  104. GetReferenceString := '%hi(' + GetReferenceString + ')';
  105. addr_low:
  106. GetReferenceString := '%lo(' + GetReferenceString + ')';
  107. addr_pic:
  108. begin
  109. if hasgot then
  110. GetReferenceString := gotprefix + GetReferenceString + ')'
  111. else
  112. internalerror(2012070401);
  113. end;
  114. end;
  115. end
  116. else
  117. begin
  118. {$ifdef extdebug}
  119. if assigned(symbol) and
  120. not(refaddr in [addr_pic,addr_low]) then
  121. internalerror(2003052601);
  122. {$endif extdebug}
  123. if base <> NR_NO then
  124. GetReferenceString := GetReferenceString + '(' + asm_regname(base) + ')';
  125. if index = NR_NO then
  126. begin
  127. if offset <> 0 then
  128. GetReferenceString := ToStr(offset) + GetReferenceString;
  129. if assigned(symbol) then
  130. begin
  131. if refaddr = addr_low then
  132. GetReferenceString := '%lo(' + symbol.Name + ')' + GetReferenceString
  133. else if refaddr = addr_pic then
  134. begin
  135. if symbol.typ=AT_FUNCTION then
  136. gotprefix:='%call16('
  137. else
  138. gotprefix:='%got(';
  139. GetReferenceString := gotprefix + symbol.Name + ')' + GetReferenceString;
  140. end
  141. else
  142. GetReferenceString := symbol.Name + {'+' +} GetReferenceString;
  143. end;
  144. end
  145. else
  146. begin
  147. {$ifdef extdebug}
  148. if (Offset<>0) or assigned(symbol) then
  149. internalerror(2003052603);
  150. {$endif extdebug}
  151. GetReferenceString := GetReferenceString + '(' + asm_regname(index) + ')';
  152. end;
  153. end;
  154. end;
  155. end;
  156. function getopstr(const Oper: TOper): string;
  157. begin
  158. with Oper do
  159. case typ of
  160. top_reg:
  161. getopstr := asm_regname(reg);
  162. top_const:
  163. getopstr := tostr(longint(val));
  164. top_ref:
  165. if (oper.ref^.refaddr in [addr_no, addr_pic]) or ((oper.ref^.refaddr = addr_low) and ((oper.ref^.base <> NR_NO) or
  166. (oper.ref^.index <> NR_NO))) then
  167. getopstr := getreferencestring(ref^)
  168. else
  169. getopstr := getreferencestring(ref^);
  170. else
  171. internalerror(10001);
  172. end;
  173. end;
  174. function getopstr_4(const Oper: TOper): string;
  175. var
  176. tmpref: treference;
  177. begin
  178. with Oper do
  179. case typ of
  180. top_ref:
  181. begin
  182. tmpref := ref^;
  183. Inc(tmpref.offset, 4);
  184. getopstr_4 := getreferencestring(tmpref);
  185. end;
  186. else
  187. internalerror(2007050403);
  188. end;
  189. end;
  190. {
  191. function getnextfpreg(tmpfpu : shortstring) : shortstring;
  192. begin
  193. case length(tmpfpu) of
  194. 3:
  195. if (tmpfpu[3] = '9') then
  196. tmpfpu:='$f10'
  197. else
  198. tmpfpu[3] := succ(tmpfpu[3]);
  199. 4:
  200. if (tmpfpu[4] = '9') then
  201. tmpfpu:='$f20'
  202. else
  203. tmpfpu[4] := succ(tmpfpu[4]);
  204. else
  205. internalerror(20120531);
  206. end;
  207. getnextfpreg := tmpfpu;
  208. end;
  209. }
  210. function is_macro_instruction(op : TAsmOp) : boolean;
  211. begin
  212. is_macro_instruction :=
  213. (op=A_SEQ) or (op=A_SNE);
  214. end;
  215. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  216. var
  217. Op: TAsmOp;
  218. s,s1: string;
  219. i: integer;
  220. tmpfpu: string;
  221. tmpfpu_len: longint;
  222. r: TRegister;
  223. begin
  224. if hp.typ <> ait_instruction then
  225. exit;
  226. op := taicpu(hp).opcode;
  227. case op of
  228. A_P_SET_NOMIPS16:
  229. begin
  230. s := #9 + '.set' + #9 + 'nomips16';
  231. owner.AsmWriteLn(s);
  232. end;
  233. A_P_MASK,
  234. A_P_FMASK:
  235. begin
  236. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  237. owner.AsmWriteLn(s);
  238. end;
  239. A_P_SET_MACRO:
  240. begin
  241. s := #9 + '.set' + #9 + 'macro';
  242. owner.AsmWriteLn(s);
  243. TMIPSGNUAssembler(owner).nomacro:=false;
  244. end;
  245. A_P_SET_REORDER:
  246. begin
  247. s := #9 + '.set' + #9 + 'reorder';
  248. owner.AsmWriteLn(s);
  249. TMIPSGNUAssembler(owner).noreorder:=false;
  250. end;
  251. A_P_SET_NOMACRO:
  252. begin
  253. s := #9 + '.set' + #9 + 'nomacro';
  254. owner.AsmWriteLn(s);
  255. TMIPSGNUAssembler(owner).nomacro:=true;
  256. end;
  257. A_P_SET_NOREORDER:
  258. begin
  259. s := #9 + '.set' + #9 + 'noreorder';
  260. owner.AsmWriteLn(s);
  261. TMIPSGNUAssembler(owner).noreorder:=true;
  262. end;
  263. A_P_SW:
  264. begin
  265. s := #9 + gas_op2str[A_SW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  266. owner.AsmWriteLn(s);
  267. end;
  268. A_P_LW:
  269. begin
  270. s := #9 + gas_op2str[A_LW] + #9 + getopstr(taicpu(hp).oper[0]^)+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  271. owner.AsmWriteLn(s);
  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(op) 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(op) 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: '-mips2 $NOWARN -EL $PIC -o $OBJ $ASM';
  345. supported_targets: [system_mipsel_linux];
  346. flags: [af_allowdirect, 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: '-mips2 $NOWARN -EB $PIC -o $OBJ $ASM';
  357. supported_targets: [system_mipseb_linux];
  358. flags: [af_allowdirect, 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.