agavrgas.pas 6.2 KB

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