agavrgas.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var
  56. op: TAsmOp;
  57. function getreferencestring(var ref : treference) : string;
  58. var
  59. s : string;
  60. begin
  61. s:='';
  62. with ref do
  63. begin
  64. {$ifdef extdebug}
  65. // if base=NR_NO then
  66. // internalerror(200308292);
  67. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  68. // internalerror(200308293);
  69. {$endif extdebug}
  70. if index<>NR_NO then
  71. internalerror(2011021701)
  72. else if base<>NR_NO then
  73. begin
  74. if addressmode=AM_PREDECREMENT then
  75. s:='-';
  76. case base of
  77. NR_R26:
  78. s:=s+'X';
  79. NR_R28:
  80. s:=s+'Y';
  81. NR_R30:
  82. s:=s+'Z';
  83. else
  84. s:=s+gas_regname(base);
  85. end;
  86. if addressmode=AM_POSTINCREMENT then
  87. s:=s+'+'
  88. else if addressmode = AM_UNCHANGED then
  89. begin
  90. if (offset>0) or ((offset=0) and (op in [A_LDD,A_STD])) then
  91. s:=s+'+'+tostr(offset)
  92. else if offset<0 then
  93. s:=s+tostr(offset);
  94. end;
  95. end
  96. else if assigned(symbol) or (offset<>0) then
  97. begin
  98. if assigned(symbol) then
  99. s:=ReplaceForbiddenAsmSymbolChars(symbol.name);
  100. if s='' then
  101. s:=tostr(offset)
  102. else if offset<0 then
  103. s:=s+tostr(offset)
  104. else if offset>0 then
  105. s:=s+'+'+tostr(offset);
  106. case refaddr of
  107. addr_hi8:
  108. s:='hi8('+s+')';
  109. addr_hi8_gs:
  110. s:='hi8(gs('+s+'))';
  111. addr_lo8:
  112. s:='lo8('+s+')';
  113. addr_lo8_gs:
  114. s:='lo8(gs('+s+'))';
  115. else
  116. s:='('+s+')';
  117. end;
  118. end
  119. { reference to address 0? }
  120. else if not(assigned(symbol)) and (offset=0) then
  121. s:='(0)';
  122. end;
  123. getreferencestring:=s;
  124. end;
  125. function getopstr(const o:toper) : string;
  126. var
  127. hs : string;
  128. first : boolean;
  129. r : tsuperregister;
  130. begin
  131. case o.typ of
  132. top_reg:
  133. getopstr:=gas_regname(o.reg);
  134. top_const:
  135. getopstr:=tostr(longint(o.val));
  136. top_ref:
  137. if o.ref^.refaddr=addr_full then
  138. begin
  139. hs:=ReplaceForbiddenAsmSymbolChars(o.ref^.symbol.name);
  140. if o.ref^.offset>0 then
  141. hs:=hs+'+'+tostr(o.ref^.offset)
  142. else if o.ref^.offset<0 then
  143. hs:=hs+tostr(o.ref^.offset);
  144. getopstr:=hs;
  145. end
  146. else
  147. getopstr:=getreferencestring(o.ref^);
  148. else
  149. internalerror(2002070604);
  150. end;
  151. end;
  152. var
  153. s: string;
  154. i: byte;
  155. sep: string[3];
  156. begin
  157. op:=taicpu(hp).opcode;
  158. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  159. if taicpu(hp).ops<>0 then
  160. begin
  161. sep:=#9;
  162. for i:=0 to taicpu(hp).ops-1 do
  163. begin
  164. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  165. sep:=',';
  166. end;
  167. end;
  168. owner.writer.AsmWriteLn(s);
  169. end;
  170. function TAVRGNUAssembler.MakeCmdLine: TCmdStr;
  171. begin
  172. result := '-mmcu='+lower(cputypestr[current_settings.cputype])+' '+inherited MakeCmdLine;
  173. end;
  174. const
  175. as_avr_gas_info : tasminfo =
  176. (
  177. id : as_gas;
  178. idtxt : 'AS';
  179. asmbin : 'as';
  180. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  181. supported_targets : [system_avr_embedded];
  182. flags : [af_needar,af_smartlink_sections];
  183. labelprefix : '.L';
  184. comment : '# ';
  185. dollarsign: 's';
  186. );
  187. begin
  188. RegisterAssembler(as_avr_gas_info,TAVRGNUAssembler);
  189. end.