agavrgas.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. aasmtai,aasmdata,
  24. aggas,
  25. cpubase;
  26. type
  27. TAVRGNUAssembler=class(TGNUassembler)
  28. constructor create(smart: boolean); override;
  29. end;
  30. TAVRInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. const
  34. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  35. '','lsl','lsr','asr','ror','rrx');
  36. implementation
  37. uses
  38. cutils,globals,verbose,
  39. systems,
  40. assemble,
  41. aasmcpu,
  42. itcpugas,
  43. cgbase,cgutils;
  44. {****************************************************************************}
  45. { GNU Arm Assembler writer }
  46. {****************************************************************************}
  47. constructor TAVRGNUAssembler.create(smart: boolean);
  48. begin
  49. inherited create(smart);
  50. InstrWriter := TAVRInstrWriter.create(self);
  51. end;
  52. {****************************************************************************}
  53. { Helper routines for Instruction Writer }
  54. {****************************************************************************}
  55. function getreferencestring(var ref : treference) : string;
  56. var
  57. s : string;
  58. begin
  59. with ref do
  60. begin
  61. {$ifdef extdebug}
  62. // if base=NR_NO then
  63. // internalerror(200308292);
  64. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  65. // internalerror(200308293);
  66. {$endif extdebug}
  67. if assigned(symbol) then
  68. begin
  69. if (base<>NR_NO) and not(is_pc(base)) then
  70. internalerror(200309011);
  71. s:=symbol.name;
  72. if offset<0 then
  73. s:=s+tostr(offset)
  74. else if offset>0 then
  75. s:=s+'+'+tostr(offset);
  76. end
  77. else
  78. begin
  79. s:=gas_regname(base);
  80. end;
  81. end;
  82. getreferencestring:=s;
  83. end;
  84. const
  85. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  86. function getopstr(const o:toper) : string;
  87. var
  88. hs : string;
  89. first : boolean;
  90. r : tsuperregister;
  91. begin
  92. case o.typ of
  93. top_reg:
  94. getopstr:=gas_regname(o.reg);
  95. top_const:
  96. getopstr:='#'+tostr(longint(o.val));
  97. top_ref:
  98. if o.ref^.refaddr=addr_full then
  99. begin
  100. hs:=o.ref^.symbol.name;
  101. if o.ref^.offset>0 then
  102. hs:=hs+'+'+tostr(o.ref^.offset)
  103. else
  104. if o.ref^.offset<0 then
  105. hs:=hs+tostr(o.ref^.offset);
  106. getopstr:=hs;
  107. end
  108. else
  109. getopstr:=getreferencestring(o.ref^);
  110. else
  111. internalerror(2002070604);
  112. end;
  113. end;
  114. Procedure TAVRInstrWriter.WriteInstruction(hp : tai);
  115. var op: TAsmOp;
  116. s: string;
  117. i: byte;
  118. sep: string[3];
  119. begin
  120. op:=taicpu(hp).opcode;
  121. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  122. if taicpu(hp).ops<>0 then
  123. begin
  124. sep:=#9;
  125. for i:=0 to taicpu(hp).ops-1 do
  126. begin
  127. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  128. sep:=',';
  129. end;
  130. end;
  131. owner.AsmWriteLn(s);
  132. end;
  133. const
  134. as_arm_gas_info : tasminfo =
  135. (
  136. id : as_gas;
  137. idtxt : 'AS';
  138. asmbin : 'as';
  139. asmcmd : '-o $OBJ $ASM';
  140. supported_targets : [system_avr_embedded];
  141. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  142. labelprefix : '.L';
  143. comment : '# ';
  144. );
  145. begin
  146. RegisterAssembler(as_arm_gas_info,TAVRGNUAssembler);
  147. end.