cpugas.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. 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.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, 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. procedure TMIPSInstrWriter.WriteInstruction(hp: Tai);
  149. var
  150. Op: TAsmOp;
  151. s: string;
  152. i: integer;
  153. begin
  154. if hp.typ <> ait_instruction then
  155. exit;
  156. op := taicpu(hp).opcode;
  157. case op of
  158. A_P_SET_NOMIPS16:
  159. begin
  160. owner.writer.AsmWriteLn(#9'.set'#9'nomips16');
  161. end;
  162. A_P_MASK,
  163. A_P_FMASK:
  164. begin
  165. s := #9 + gas_op2str[op] + #9'0x' + hexstr(taicpu(hp).oper[0]^.val,8)+ ',' + getopstr(taicpu(hp).oper[1]^) ;
  166. owner.writer.AsmWriteLn(s);
  167. end;
  168. A_P_SET_MACRO:
  169. begin
  170. owner.writer.AsmWriteLn(#9'.set'#9'macro');
  171. TMIPSGNUAssembler(owner).nomacro:=false;
  172. end;
  173. A_P_SET_REORDER:
  174. begin
  175. owner.writer.AsmWriteLn(#9'.set'#9'reorder');
  176. TMIPSGNUAssembler(owner).noreorder:=false;
  177. end;
  178. A_P_SET_NOMACRO:
  179. begin
  180. owner.writer.AsmWriteLn(#9'.set'#9'nomacro');
  181. TMIPSGNUAssembler(owner).nomacro:=true;
  182. end;
  183. A_P_SET_NOREORDER:
  184. begin
  185. owner.writer.AsmWriteLn(#9'.set'#9'noreorder');
  186. TMIPSGNUAssembler(owner).noreorder:=true;
  187. end;
  188. A_P_SET_NOAT:
  189. begin
  190. owner.writer.AsmWriteln(#9'.set'#9'noat');
  191. TMIPSGNUAssembler(owner).noat:=true;
  192. end;
  193. A_P_SET_AT:
  194. begin
  195. owner.writer.AsmWriteln(#9'.set'#9'at');
  196. TMIPSGNUAssembler(owner).noat:=false;
  197. end;
  198. else
  199. begin
  200. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  201. owner.writer.AsmWriteln(#9'.set'#9'macro');
  202. s := #9 + gas_op2str[op] + cond2str[taicpu(hp).condition];
  203. if taicpu(hp).ops > 0 then
  204. begin
  205. s := s + #9 + getopstr(taicpu(hp).oper[0]^);
  206. for i := 1 to taicpu(hp).ops - 1 do
  207. s := s + ',' + getopstr(taicpu(hp).oper[i]^);
  208. end;
  209. owner.writer.AsmWriteLn(s);
  210. if taicpu(hp).is_macro and TMIPSGNUAssembler(owner).nomacro then
  211. owner.writer.AsmWriteln(#9'.set'#9'nomacro');
  212. end;
  213. end;
  214. end;
  215. const
  216. {$ifdef MIPSEL}
  217. as_MIPSEL_as_info: tasminfo =
  218. (
  219. id: as_gas;
  220. idtxt: 'AS';
  221. asmbin: 'as';
  222. asmcmd: '$ABI $ARCH $NOWARN -EL $PIC -o $OBJ $EXTRAOPT $ASM';
  223. supported_targets: [system_mipsel_linux,system_mipsel_android,system_mipsel_embedded];
  224. flags: [ af_needar, af_smartlink_sections];
  225. labelprefix: '.L';
  226. comment: '# ';
  227. dollarsign: '$';
  228. );
  229. {$else MIPSEL}
  230. as_MIPSEB_as_info: tasminfo =
  231. (
  232. id: as_gas;
  233. idtxt: 'AS';
  234. asmbin: 'as';
  235. asmcmd: '$ABI $ARCH $NOWARN -EB $PIC -o $OBJ $EXTRAOPT $ASM';
  236. supported_targets: [system_mipseb_linux];
  237. flags: [ af_needar, af_smartlink_sections];
  238. labelprefix: '.L';
  239. comment: '# ';
  240. dollarsign: '$';
  241. );
  242. {$endif MIPSEL}
  243. begin
  244. {$ifdef MIPSEL}
  245. RegisterAssembler(as_MIPSEL_as_info, TMIPSGNUAssembler);
  246. {$else MIPSEL}
  247. RegisterAssembler(as_MIPSEB_as_info, TMIPSGNUAssembler);
  248. {$endif MIPSEL}
  249. end.