agarmgas.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 agarmgas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. aasmtai,
  24. aggas,
  25. cpubase;
  26. type
  27. PARMGNUAssembler=^TARMGNUAssembler;
  28. TARMGNUAssembler=class(TGNUassembler)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. const
  32. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  33. '','lsl','lsr','asr','ror','rrx');
  34. implementation
  35. uses
  36. cutils,globals,verbose,
  37. systems,
  38. assemble,
  39. aasmcpu,
  40. itcpugas,
  41. cgbase,cgutils;
  42. const
  43. as_arm_gas_info : tasminfo =
  44. (
  45. id : as_gas;
  46. idtxt : 'AS';
  47. asmbin : 'as';
  48. asmcmd : '-o $OBJ $ASM';
  49. supported_target : system_any;
  50. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  51. labelprefix : '.L';
  52. comment : '# ';
  53. );
  54. function getreferencestring(var ref : treference) : string;
  55. var
  56. s : string;
  57. begin
  58. with ref do
  59. begin
  60. {$ifdef extdebug}
  61. // if base=NR_NO then
  62. // internalerror(200308292);
  63. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  64. // internalerror(200308293);
  65. {$endif extdebug}
  66. if assigned(symbol) then
  67. begin
  68. if (base<>NR_NO) and not(is_pc(base)) then
  69. internalerror(200309011);
  70. s:=symbol.name;
  71. if offset<0 then
  72. s:=s+tostr(offset)
  73. else if offset>0 then
  74. s:=s+'+'+tostr(offset);
  75. end
  76. else
  77. begin
  78. s:='['+gas_regname(base);
  79. if addressmode=AM_POSTINDEXED then
  80. s:=s+']';
  81. if index<>NR_NO then
  82. begin
  83. if signindex<0 then
  84. s:=s+', -'
  85. else
  86. s:=s+', ';
  87. s:=s+gas_regname(index);
  88. if shiftmode<>SM_None then
  89. s:=s+' ,'+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  90. end
  91. else if offset<>0 then
  92. s:=s+', #'+tostr(offset);
  93. case addressmode of
  94. AM_OFFSET:
  95. s:=s+']';
  96. AM_PREINDEXED:
  97. s:=s+']!';
  98. end;
  99. end;
  100. end;
  101. getreferencestring:=s;
  102. end;
  103. const
  104. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  105. function getopstr(const o:toper) : string;
  106. var
  107. hs : string;
  108. first : boolean;
  109. r : tsuperregister;
  110. begin
  111. case o.typ of
  112. top_reg:
  113. getopstr:=gas_regname(o.reg);
  114. top_shifterop:
  115. begin
  116. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  117. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  118. else if (o.shifterop^.rs=NR_NO) then
  119. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  120. else internalerror(200308282);
  121. end;
  122. top_const:
  123. getopstr:='#'+tostr(longint(o.val));
  124. top_regset:
  125. begin
  126. getopstr:='{';
  127. first:=true;
  128. for r:=RS_R0 to RS_R15 do
  129. if r in o.regset^ then
  130. begin
  131. if not(first) then
  132. getopstr:=getopstr+',';
  133. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  134. first:=false;
  135. end;
  136. getopstr:=getopstr+'}';
  137. end;
  138. top_ref:
  139. if o.ref^.refaddr=addr_full then
  140. begin
  141. hs:=o.ref^.symbol.name;
  142. if o.ref^.offset>0 then
  143. hs:=hs+'+'+tostr(o.ref^.offset)
  144. else
  145. if o.ref^.offset<0 then
  146. hs:=hs+tostr(o.ref^.offset);
  147. getopstr:=hs;
  148. end
  149. else
  150. getopstr:=getreferencestring(o.ref^);
  151. else
  152. internalerror(2002070604);
  153. end;
  154. end;
  155. Procedure TARMGNUAssembler.WriteInstruction(hp : tai);
  156. var op: TAsmOp;
  157. s: string;
  158. i: byte;
  159. sep: string[3];
  160. begin
  161. op:=taicpu(hp).opcode;
  162. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  163. if taicpu(hp).ops<>0 then
  164. begin
  165. sep:=#9;
  166. for i:=0 to taicpu(hp).ops-1 do
  167. begin
  168. // debug code
  169. // writeln(s);
  170. // writeln(taicpu(hp).fileinfo.line);
  171. { LDM and STM use references as first operand but they are written like a register }
  172. if (i=0) and (op in [A_LDM,A_STM]) then
  173. begin
  174. case taicpu(hp).oper[0]^.typ of
  175. top_ref:
  176. begin
  177. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  178. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  179. s:=s+'!';
  180. end;
  181. top_reg:
  182. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  183. else
  184. internalerror(200311292);
  185. end;
  186. end
  187. { register count of SFM and LFM is written without # }
  188. else if (i=1) and (op in [A_SFM,A_LFM]) then
  189. begin
  190. case taicpu(hp).oper[1]^.typ of
  191. top_const:
  192. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  193. else
  194. internalerror(200311292);
  195. end;
  196. end
  197. else
  198. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  199. sep:=',';
  200. end;
  201. end;
  202. AsmWriteLn(s);
  203. end;
  204. begin
  205. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  206. end.