cpugas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  27. {# Constructs the command line for calling the assembler }
  28. function MakeCmdLine: TCmdStr; override;
  29. procedure WriteExtraHeader; override;
  30. end;
  31. TMIPSInstrWriter = class(TCPUInstrWriter)
  32. procedure WriteInstruction(hp : tai);override;
  33. end;
  34. const
  35. use_std_regnames : boolean =
  36. {$ifndef USE_MIPS_GAS_REGS}
  37. true;
  38. {$else}
  39. false;
  40. {$endif}
  41. implementation
  42. uses
  43. cutils, cpuinfo,
  44. globals, verbose, itcpugas, cgbase, cgutils;
  45. { float related options as accepted by
  46. GNU assembler in -mXXXX option:
  47. -mhard-float allow floating-point instructions
  48. -msoft-float do not allow floating-point instructions
  49. -msingle-float only allow 32-bit floating-point operations
  50. -mdouble-float allow 32-bit and 64-bit floating-point operations
  51. }
  52. function gas_float_option(fputype : tfputype) : string;
  53. begin
  54. case fputype of
  55. fpu_none:
  56. result:='';
  57. fpu_soft:
  58. result:='-msoft-float';
  59. else
  60. result:='-mhard-float';
  61. end;
  62. end;
  63. { abi strings as accepted by
  64. GNU assembler in -abi=XXX option }
  65. function gas_abitype(abi : tabi) : string;
  66. begin
  67. case abi of
  68. abi_eabi:
  69. result:='eabi';
  70. abi_mips_o32:
  71. result:='32';
  72. abi_mips_n32:
  73. result:='n32';
  74. abi_mips_o64:
  75. result:='o64';
  76. abi_mips_n64:
  77. result:='64';
  78. else
  79. result:='32';
  80. end;
  81. end;
  82. function asm_regname(reg : TRegister) : string;
  83. begin
  84. if use_std_regnames then
  85. asm_regname:='$'+std_regname(reg)
  86. else
  87. asm_regname:=gas_regname(reg);
  88. end;
  89. {****************************************************************************}
  90. { GNU MIPS Assembler writer }
  91. {****************************************************************************}
  92. constructor TMIPSGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  93. begin
  94. inherited;
  95. InstrWriter:=TMIPSInstrWriter.create(self);
  96. nomacro:=false;
  97. noreorder:=false;
  98. noat:=false;
  99. end;
  100. function TMIPSGNUAssembler.MakeCmdLine: TCmdStr;
  101. begin
  102. result := Inherited MakeCmdLine;
  103. { ABI selection }
  104. Replace(result,'$ABI','-mabi='+gas_abitype(target_info.abi));
  105. { float selection }
  106. Replace(result,'$FLOATABI',gas_float_option(current_settings.fputype));
  107. { ARCH selection }
  108. Replace(result,'$ARCH','-march='+lower(cputypestr[current_settings.cputype]));
  109. // Replace(result,'$ARCH','-march=pic32mx -mtune=pic32mx');
  110. end;
  111. procedure TMIPSGNUAssembler.WriteExtraHeader;
  112. begin
  113. if not (cs_asm_pre_binutils_2_25 in current_settings.globalswitches) then
  114. writer.AsmWriteln(#9'.module nomips16');
  115. end;
  116. {****************************************************************************}
  117. { Helper routines for Instruction Writer }
  118. {****************************************************************************}
  119. function GetReferenceString(var ref: TReference): string;
  120. var
  121. reg: TRegister;
  122. regstr: string;
  123. begin
  124. result:='';
  125. if assigned(ref.symbol) then
  126. result:=ref.symbol.name;
  127. if (ref.offset<0) then
  128. result:=result+tostr(ref.offset)
  129. else if (ref.offset>0) then
  130. begin
  131. if assigned(ref.symbol) then
  132. result:=result+'+';
  133. result:=result+tostr(ref.offset);
  134. end
  135. { asmreader appears to treat literal numbers as references }
  136. else if (ref.symbol=nil) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  137. result:='0';
  138. { either base or index may be present, but not both }
  139. reg:=ref.base;
  140. if (reg=NR_NO) then
  141. reg:=ref.index
  142. else if (ref.index<>NR_NO) then
  143. InternalError(2013013001);
  144. if (reg=NR_NO) then
  145. regstr:=''
  146. else
  147. regstr:='('+asm_regname(reg)+')';
  148. case ref.refaddr of
  149. addr_no,
  150. addr_full:
  151. if assigned(ref.symbol) and (reg<>NR_NO) then
  152. InternalError(2013013002)
  153. else
  154. begin
  155. result:=result+regstr;
  156. exit;
  157. end;
  158. addr_pic:
  159. result:='%got('+result;
  160. addr_high:
  161. result:='%hi('+result;
  162. addr_low:
  163. result:='%lo('+result;
  164. addr_pic_call16:
  165. result:='%call16('+result;
  166. addr_low_pic:
  167. result:='%got_lo('+result;
  168. addr_high_pic:
  169. result:='%got_hi('+result;
  170. addr_low_call:
  171. result:='%call_lo('+result;
  172. addr_high_call:
  173. result:='%call_hi('+result;
  174. else
  175. InternalError(2013013003);
  176. end;
  177. result:=result+')'+regstr;
  178. end;
  179. function getopstr(const Oper: TOper): string;
  180. begin
  181. with Oper do
  182. case typ of
  183. top_reg:
  184. getopstr := asm_regname(reg);
  185. top_const:
  186. getopstr := tostr(longint(val));
  187. top_ref:
  188. getopstr := getreferencestring(ref^);
  189. else
  190. internalerror(2020100809);
  191. end;
  192. end;
  193. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  194. var
  195. Op: TAsmOp;
  196. s: string;
  197. i: integer;
  198. begin
  199. if hp.typ <> ait_instruction then
  200. exit;
  201. op := taicpu(hp).opcode;
  202. case op of
  203. A_P_SET_NOMIPS16:
  204. begin
  205. owner.writer.AsmWriteLn(#9'.set'#9'nomips16');
  206. end;
  207. A_P_MASK,
  208. A_P_FMASK:
  209. begin
  210. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  211. owner.writer.AsmWriteLn(s);
  212. end;
  213. A_P_SET_MACRO:
  214. begin
  215. owner.writer.AsmWriteLn(#9'.set'#9'macro');
  216. TMIPSGNUAssembler(owner).nomacro:=false;
  217. end;
  218. A_P_SET_REORDER:
  219. begin
  220. owner.writer.AsmWriteLn(#9'.set'#9'reorder');
  221. TMIPSGNUAssembler(owner).noreorder:=false;
  222. end;
  223. A_P_SET_NOMACRO:
  224. begin
  225. owner.writer.AsmWriteLn(#9'.set'#9'nomacro');
  226. TMIPSGNUAssembler(owner).nomacro:=true;
  227. end;
  228. A_P_SET_NOREORDER:
  229. begin
  230. owner.writer.AsmWriteLn(#9'.set'#9'noreorder');
  231. TMIPSGNUAssembler(owner).noreorder:=true;
  232. end;
  233. A_P_SET_NOAT:
  234. begin
  235. owner.writer.AsmWriteln(#9'.set'#9'noat');
  236. TMIPSGNUAssembler(owner).noat:=true;
  237. end;
  238. A_P_SET_AT:
  239. begin
  240. owner.writer.AsmWriteln(#9'.set'#9'at');
  241. TMIPSGNUAssembler(owner).noat:=false;
  242. end;
  243. else
  244. begin
  245. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  246. owner.writer.AsmWriteln(#9'.set'#9'macro');
  247. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  248. if taicpu(hp).ops > 0 then
  249. begin
  250. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  251. for i := 1 to taicpu(hp).ops - 1 do
  252. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  253. end;
  254. owner.writer.AsmWriteLn(s);
  255. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  256. owner.writer.AsmWriteln(#9'.set'#9'nomacro');
  257. end;
  258. end;
  259. end;
  260. const
  261. {$ifdef MIPSEL}
  262. as_MIPSEL_as_info: tasminfo =
  263. (
  264. id: as_gas;
  265. idtxt: 'AS';
  266. asmbin: 'as';
  267. asmcmd: '$ABI $FLOATABI $ARCH $NOWARN -EL $PIC -o $OBJ $EXTRAOPT $ASM';
  268. supported_targets: [system_mipsel_linux,system_mipsel_android,system_mipsel_embedded,system_mipsel_ps1];
  269. flags: [ af_needar, af_smartlink_sections];
  270. labelprefix: '.L';
  271. labelmaxlen : -1;
  272. comment: '# ';
  273. dollarsign: '$';
  274. );
  275. {$else MIPSEL}
  276. as_MIPSEB_as_info: tasminfo =
  277. (
  278. id: as_gas;
  279. idtxt: 'AS';
  280. asmbin: 'as';
  281. asmcmd: '$ABI $FLOATABI $ARCH $NOWARN -EB $PIC -o $OBJ $EXTRAOPT $ASM';
  282. supported_targets: [system_mipseb_linux];
  283. flags: [ af_needar, af_smartlink_sections];
  284. labelprefix: '.L';
  285. labelmaxlen : -1;
  286. comment: '# ';
  287. dollarsign: '$';
  288. );
  289. {$endif MIPSEL}
  290. begin
  291. {$ifdef MIPSEL}
  292. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  293. {$else MIPSEL}
  294. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  295. {$endif MIPSEL}
  296. end.