2
0

agcpugas.pas 5.9 KB

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