agcpugas.pas 5.7 KB

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