cpugas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  185. var
  186. Op: TAsmOp;
  187. s: string;
  188. i: integer;
  189. tmpfpu: string;
  190. //tmpfpu_len: longint;
  191. r: TRegister;
  192. begin
  193. if hp.typ <> ait_instruction then
  194. exit;
  195. op := taicpu(hp).opcode;
  196. case op of
  197. A_P_SET_NOMIPS16:
  198. begin
  199. owner.writer.AsmWriteLn(#9'.set'#9'nomips16');
  200. end;
  201. A_P_MASK,
  202. A_P_FMASK:
  203. begin
  204. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  205. owner.writer.AsmWriteLn(s);
  206. end;
  207. A_P_SET_MACRO:
  208. begin
  209. owner.writer.AsmWriteLn(#9'.set'#9'macro');
  210. TMIPSGNUAssembler(owner).nomacro:=false;
  211. end;
  212. A_P_SET_REORDER:
  213. begin
  214. owner.writer.AsmWriteLn(#9'.set'#9'reorder');
  215. TMIPSGNUAssembler(owner).noreorder:=false;
  216. end;
  217. A_P_SET_NOMACRO:
  218. begin
  219. owner.writer.AsmWriteLn(#9'.set'#9'nomacro');
  220. TMIPSGNUAssembler(owner).nomacro:=true;
  221. end;
  222. A_P_SET_NOREORDER:
  223. begin
  224. owner.writer.AsmWriteLn(#9'.set'#9'noreorder');
  225. TMIPSGNUAssembler(owner).noreorder:=true;
  226. end;
  227. A_P_SET_NOAT:
  228. begin
  229. owner.writer.AsmWriteln(#9'.set'#9'noat');
  230. TMIPSGNUAssembler(owner).noat:=true;
  231. end;
  232. A_P_SET_AT:
  233. begin
  234. owner.writer.AsmWriteln(#9'.set'#9'at');
  235. TMIPSGNUAssembler(owner).noat:=false;
  236. end;
  237. A_LDC1:
  238. begin
  239. if (target_info.endian = endian_big) then
  240. begin
  241. s := #9 + gas_op2str[A_LDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  242. + ',' + getopstr(taicpu(hp).oper[1]^);
  243. end
  244. else
  245. begin
  246. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  247. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  248. owner.writer.AsmWriteLn(s);
  249. { bug if $f9/$f19
  250. tmpfpu_len := length(tmpfpu);
  251. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  252. }
  253. r := taicpu(hp).oper[0]^.reg;
  254. setsupreg(r, getsupreg(r) + 1);
  255. tmpfpu := asm_regname(r);
  256. s := #9 + gas_op2str[A_LWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); // + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  257. end;
  258. owner.writer.AsmWriteLn(s);
  259. end;
  260. A_SDC1:
  261. begin
  262. if (target_info.endian = endian_big) then
  263. begin
  264. s := #9 + gas_op2str[A_SDC1] + #9 + getopstr(taicpu(hp).oper[0]^)
  265. + ',' + getopstr(taicpu(hp).oper[1]^);
  266. end
  267. else
  268. begin
  269. tmpfpu := getopstr(taicpu(hp).oper[0]^);
  270. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  271. owner.writer.AsmWriteLn(s);
  272. {
  273. tmpfpu_len := length(tmpfpu);
  274. tmpfpu[tmpfpu_len] := succ(tmpfpu[tmpfpu_len]);
  275. }
  276. r := taicpu(hp).oper[0]^.reg;
  277. setsupreg(r, getsupreg(r) + 1);
  278. tmpfpu := asm_regname(r);
  279. s := #9 + gas_op2str[A_SWC1] + #9 + tmpfpu + ',' + getopstr_4(taicpu(hp).oper[1]^); //+ ',' + getopstr(taicpu(hp).oper[2]^) + '(' + getopstr(taicpu(hp).oper[1]^) + ')';
  280. end;
  281. owner.writer.AsmWriteLn(s);
  282. end;
  283. else
  284. begin
  285. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  286. owner.writer.AsmWriteln(#9'.set'#9'macro');
  287. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  288. if taicpu(hp).ops > 0 then
  289. begin
  290. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  291. for i := 1 to taicpu(hp).ops - 1 do
  292. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  293. end;
  294. owner.writer.AsmWriteLn(s);
  295. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  296. owner.writer.AsmWriteln(#9'.set'#9'nomacro');
  297. end;
  298. end;
  299. end;
  300. const
  301. {$ifdef MIPSEL}
  302. as_MIPSEL_as_info: tasminfo =
  303. (
  304. id: as_gas;
  305. idtxt: 'AS';
  306. asmbin: 'as';
  307. asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $EXTRAOPT $ASM';
  308. supported_targets: [system_mipsel_linux,system_mipsel_android,system_mipsel_embedded];
  309. flags: [ af_needar, af_smartlink_sections];
  310. labelprefix: '.L';
  311. comment: '# ';
  312. dollarsign: '$';
  313. );
  314. {$else MIPSEL}
  315. as_MIPSEB_as_info: tasminfo =
  316. (
  317. id: as_gas;
  318. idtxt: 'AS';
  319. asmbin: 'as';
  320. asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $EXTRAOPT $ASM';
  321. supported_targets: [system_mipseb_linux];
  322. flags: [ af_needar, af_smartlink_sections];
  323. labelprefix: '.L';
  324. comment: '# ';
  325. dollarsign: '$';
  326. );
  327. {$endif MIPSEL}
  328. begin
  329. {$ifdef MIPSEL}
  330. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  331. {$else MIPSEL}
  332. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  333. {$endif MIPSEL}
  334. end.