agcpugas.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if assigned(symbol) then
  63. begin
  64. s:=symbol.name;
  65. if offset<>0 then
  66. s:=s+tostr_with_plus(offset);
  67. case refaddr of
  68. addr_pic:
  69. s:=s+'(PLT)';
  70. addr_full,
  71. addr_no:
  72. ;
  73. else
  74. Internalerror(2020112403);
  75. end;
  76. end
  77. else
  78. begin
  79. s:=gas_regname(base);
  80. if index<>NR_NO then
  81. begin
  82. s:=s+','+gas_regname(index);
  83. if offset<>0 then
  84. Internalerror(2020112402);
  85. end
  86. else
  87. s:=s+','+tostr(offset);
  88. end;
  89. end;
  90. getreferencestring:=s;
  91. end;
  92. function getopstr(const o:toper) : string;
  93. var
  94. hs : string;
  95. first : boolean;
  96. r, rs : tsuperregister;
  97. begin
  98. case o.typ of
  99. top_reg:
  100. getopstr:=gas_regname(o.reg);
  101. top_const:
  102. getopstr:=tostr(longint(o.val));
  103. top_ref:
  104. if o.ref^.refaddr=addr_full then
  105. begin
  106. hs:=o.ref^.symbol.name;
  107. if o.ref^.offset>0 then
  108. hs:=hs+'+'+tostr(o.ref^.offset)
  109. else
  110. if o.ref^.offset<0 then
  111. hs:=hs+tostr(o.ref^.offset);
  112. getopstr:=hs;
  113. end
  114. else
  115. getopstr:=getreferencestring(o.ref^);
  116. else
  117. internalerror(2020030705);
  118. end;
  119. end;
  120. Procedure TXtensaInstrWriter.WriteInstruction(hp : tai);
  121. var op: TAsmOp;
  122. s: string;
  123. i: byte;
  124. sep: string[3];
  125. begin
  126. op:=taicpu(hp).opcode;
  127. if taicpu(hp).opIsPrefixed then
  128. s:=#9'_'+gas_op2str[op]
  129. else
  130. s:=#9+gas_op2str[op];
  131. if taicpu(hp).condition<>C_None then
  132. s:=s+cond2str[taicpu(hp).condition];
  133. if taicpu(hp).oppostfix <> PF_None then
  134. s:=s+'.'+oppostfix2str[taicpu(hp).oppostfix];
  135. if taicpu(hp).ops<>0 then
  136. begin
  137. if length(s)<5 then
  138. sep:=#9#9
  139. else
  140. sep:=#9;
  141. for i:=0 to taicpu(hp).ops-1 do
  142. begin
  143. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  144. sep:=',';
  145. end;
  146. end;
  147. owner.writer.AsmWriteLn(s);
  148. end;
  149. const
  150. as_xtensa_gas_info : tasminfo =
  151. (
  152. id : as_gas;
  153. idtxt : 'AS';
  154. asmbin : 'as';
  155. asmcmd : '-o $OBJ $EXTRAOPT $ASM --longcalls';
  156. supported_targets : [system_xtensa_embedded,system_xtensa_linux,system_xtensa_freertos];
  157. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  158. labelprefix : '.L';
  159. labelmaxlen : -1;
  160. comment : '# ';
  161. dollarsign: '$';
  162. );
  163. begin
  164. RegisterAssembler(as_xtensa_gas_info,TXtensaGNUAssembler);
  165. end.