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