cpugas.pas 8.9 KB

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