cpugas.pas 9.4 KB

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