cpugas.pas 14 KB

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