agcpugas.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the Xtensa
  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. { This unit implements the GNU Assembler writer for the Xtensa
  18. }
  19. unit agcpugas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,systems,
  24. aasmtai,
  25. assemble,aggas,
  26. cpubase,cpuinfo;
  27. type
  28. TXtensaInstrWriter=class(TCPUInstrWriter)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. TXtensaGNUAssembler=class(TGNUassembler)
  32. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  33. function MakeCmdLine: TCmdStr; override;
  34. end;
  35. implementation
  36. uses
  37. cutils,globals,verbose,
  38. aasmcpu,
  39. itcpugas,
  40. cgbase,cgutils;
  41. {****************************************************************************}
  42. { GNU Xtensa Assembler writer }
  43. {****************************************************************************}
  44. constructor TXtensaGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  45. begin
  46. inherited;
  47. InstrWriter := TXtensaInstrWriter.create(self);
  48. end;
  49. function TXtensaGNUAssembler.MakeCmdLine: TCmdStr;
  50. begin
  51. result:=inherited MakeCmdLine;
  52. end;
  53. {****************************************************************************}
  54. { Helper routines for Instruction Writer }
  55. {****************************************************************************}
  56. function getreferencestring(var ref : treference) : string;
  57. var
  58. s : string;
  59. begin
  60. with ref do
  61. begin
  62. {$ifdef extdebug}
  63. // if base=NR_NO then
  64. // internalerror(200308292);
  65. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  66. // internalerror(200308293);
  67. {$endif extdebug}
  68. if assigned(symbol) then
  69. begin
  70. s:=symbol.name;
  71. if offset<>0 then
  72. s:=s+tostr_with_plus(offset);
  73. if refaddr=addr_pic then
  74. s:=s+'(PLT)'
  75. {else if refaddr=addr_tlscall then
  76. s:=s+'(tlscall)'};
  77. end
  78. else
  79. begin
  80. s:=gas_regname(base);
  81. if index<>NR_NO then
  82. Internalerror(2020030802);
  83. s:=s+','+tostr(offset);
  84. end;
  85. end;
  86. getreferencestring:=s;
  87. end;
  88. function getopstr(const o:toper) : string;
  89. var
  90. hs : string;
  91. first : boolean;
  92. r, rs : tsuperregister;
  93. begin
  94. case o.typ of
  95. top_reg:
  96. getopstr:=gas_regname(o.reg);
  97. top_const:
  98. getopstr:=tostr(longint(o.val));
  99. top_ref:
  100. if o.ref^.refaddr=addr_full then
  101. begin
  102. hs:=o.ref^.symbol.name;
  103. if o.ref^.offset>0 then
  104. hs:=hs+'+'+tostr(o.ref^.offset)
  105. else
  106. if o.ref^.offset<0 then
  107. hs:=hs+tostr(o.ref^.offset);
  108. getopstr:=hs;
  109. end
  110. else
  111. getopstr:=getreferencestring(o.ref^);
  112. else
  113. internalerror(2020030705);
  114. end;
  115. end;
  116. Procedure TXtensaInstrWriter.WriteInstruction(hp : tai);
  117. var op: TAsmOp;
  118. postfix,s: string;
  119. i: byte;
  120. sep: string[3];
  121. begin
  122. op:=taicpu(hp).opcode;
  123. postfix:='';
  124. s:=#9+gas_op2str[op];
  125. if taicpu(hp).condition<>C_None then
  126. s:=s+cond2str[taicpu(hp).condition];
  127. if taicpu(hp).ops<>0 then
  128. begin
  129. if length(s)<5 then
  130. sep:=#9#9
  131. else
  132. sep:=#9;
  133. for i:=0 to taicpu(hp).ops-1 do
  134. begin
  135. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  136. sep:=',';
  137. end;
  138. end;
  139. owner.writer.AsmWriteLn(s);
  140. end;
  141. const
  142. as_xtensa_gas_info : tasminfo =
  143. (
  144. id : as_gas;
  145. idtxt : 'AS';
  146. asmbin : 'as';
  147. asmcmd : '-o $OBJ $EXTRAOPT $ASM --longcalls';
  148. supported_targets : [system_xtensa_embedded];
  149. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  150. labelprefix : '.L';
  151. labelmaxlen : -1;
  152. comment : '# ';
  153. dollarsign: '$';
  154. );
  155. begin
  156. RegisterAssembler(as_xtensa_gas_info,TXtensaGNUAssembler);
  157. end.