agavrgas.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the ARM
  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 ARM
  18. }
  19. unit agavrgas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,systems,
  24. aasmtai,aasmdata,
  25. assemble,aggas,
  26. cpubase;
  27. type
  28. { TAVRGNUAssembler }
  29. TAVRGNUAssembler=class(TGNUassembler)
  30. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  31. function MakeCmdLine: TCmdStr; override;
  32. end;
  33. TAVRInstrWriter=class(TCPUInstrWriter)
  34. procedure WriteInstruction(hp : tai);override;
  35. end;
  36. implementation
  37. uses
  38. cutils,globals,verbose,
  39. aasmbase,aasmcpu,
  40. itcpugas,
  41. cpuinfo,
  42. cgbase,cgutils;
  43. {****************************************************************************}
  44. { GNU Arm Assembler writer }
  45. {****************************************************************************}
  46. constructor TAVRGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  47. begin
  48. inherited;
  49. InstrWriter := TAVRInstrWriter.create(self);
  50. end;
  51. {****************************************************************************}
  52. { Helper routines for Instruction Writer }
  53. {****************************************************************************}
  54. Procedure TAVRInstrWriter.WriteInstruction(hp : tai);
  55. function getreferencestring(var ref : treference) : string;
  56. var
  57. s : string;
  58. begin
  59. s:='';
  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 index<>NR_NO then
  69. internalerror(2011021701)
  70. else if base<>NR_NO then
  71. begin
  72. if addressmode=AM_PREDRECEMENT then
  73. s:='-';
  74. case base of
  75. NR_R26:
  76. s:=s+'X';
  77. NR_R28:
  78. s:=s+'Y';
  79. NR_R30:
  80. s:=s+'Z';
  81. else
  82. s:=gas_regname(base);
  83. end;
  84. if addressmode=AM_POSTINCREMENT then
  85. s:=s+'+';
  86. if offset>0 then
  87. s:=s+'+'+tostr(offset)
  88. else if offset<0 then
  89. s:=s+tostr(offset)
  90. end
  91. else if assigned(symbol) or (offset<>0) then
  92. begin
  93. if assigned(symbol) then
  94. s:=ReplaceForbiddenAsmSymbolChars(symbol.name);
  95. if offset<0 then
  96. s:=s+tostr(offset)
  97. else if offset>0 then
  98. s:=s+'+'+tostr(offset);
  99. case refaddr of
  100. addr_hi8:
  101. s:='hi8('+s+')';
  102. addr_hi8_gs:
  103. s:='hi8(gs('+s+'))';
  104. addr_lo8:
  105. s:='lo8('+s+')';
  106. addr_lo8_gs:
  107. s:='lo8(gs('+s+'))';
  108. else
  109. s:='('+s+')';
  110. end;
  111. end;
  112. end;
  113. getreferencestring:=s;
  114. end;
  115. function getopstr(const o:toper) : string;
  116. var
  117. hs : string;
  118. first : boolean;
  119. r : tsuperregister;
  120. begin
  121. case o.typ of
  122. top_reg:
  123. getopstr:=gas_regname(o.reg);
  124. top_const:
  125. getopstr:=tostr(longint(o.val));
  126. top_ref:
  127. if o.ref^.refaddr=addr_full then
  128. begin
  129. hs:=ReplaceForbiddenAsmSymbolChars(o.ref^.symbol.name);
  130. if o.ref^.offset>0 then
  131. hs:=hs+'+'+tostr(o.ref^.offset)
  132. else
  133. if o.ref^.offset<0 then
  134. hs:=hs+tostr(o.ref^.offset);
  135. getopstr:=hs;
  136. end
  137. else
  138. getopstr:=getreferencestring(o.ref^);
  139. else
  140. internalerror(2002070604);
  141. end;
  142. end;
  143. var op: TAsmOp;
  144. s: string;
  145. i: byte;
  146. sep: string[3];
  147. begin
  148. op:=taicpu(hp).opcode;
  149. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  150. if taicpu(hp).ops<>0 then
  151. begin
  152. sep:=#9;
  153. for i:=0 to taicpu(hp).ops-1 do
  154. begin
  155. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  156. sep:=',';
  157. end;
  158. end;
  159. owner.writer.AsmWriteLn(s);
  160. end;
  161. function TAVRGNUAssembler.MakeCmdLine: TCmdStr;
  162. begin
  163. result := '-mmcu='+lower(cputypestr[current_settings.cputype])+' '+inherited MakeCmdLine;
  164. end;
  165. const
  166. as_avr_gas_info : tasminfo =
  167. (
  168. id : as_gas;
  169. idtxt : 'AS';
  170. asmbin : 'as';
  171. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  172. supported_targets : [system_avr_embedded];
  173. flags : [af_needar,af_smartlink_sections];
  174. labelprefix : '.L';
  175. comment : '# ';
  176. dollarsign: 's';
  177. );
  178. begin
  179. RegisterAssembler(as_avr_gas_info,TAVRGNUAssembler);
  180. end.