agarmgas.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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,aasmdata,
  24. aggas,
  25. cpubase;
  26. type
  27. TARMGNUAssembler=class(TGNUassembler)
  28. constructor create(smart: boolean); override;
  29. end;
  30. TArmInstrWriter=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 TArmGNUAssembler.create(smart: boolean);
  48. begin
  49. inherited create(smart);
  50. InstrWriter := TArmInstrWriter.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. if addressmode=AM_POSTINDEXED then
  81. s:=s+']';
  82. if index<>NR_NO then
  83. begin
  84. if signindex<0 then
  85. s:=s+', -'
  86. else
  87. s:=s+', ';
  88. s:=s+gas_regname(index);
  89. if shiftmode<>SM_None then
  90. s:=s+', '+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  91. end
  92. else if offset<>0 then
  93. s:=s+', #'+tostr(offset);
  94. case addressmode of
  95. AM_OFFSET:
  96. s:=s+']';
  97. AM_PREINDEXED:
  98. s:=s+']!';
  99. end;
  100. end;
  101. end;
  102. getreferencestring:=s;
  103. end;
  104. const
  105. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  106. function getopstr(const o:toper) : string;
  107. var
  108. hs : string;
  109. first : boolean;
  110. r : tsuperregister;
  111. begin
  112. case o.typ of
  113. top_reg:
  114. getopstr:=gas_regname(o.reg);
  115. top_shifterop:
  116. begin
  117. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  118. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  119. else if (o.shifterop^.rs=NR_NO) then
  120. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  121. else internalerror(200308282);
  122. end;
  123. top_const:
  124. getopstr:='#'+tostr(longint(o.val));
  125. top_regset:
  126. begin
  127. getopstr:='{';
  128. first:=true;
  129. for r:=RS_R0 to RS_R15 do
  130. if r in o.regset^ then
  131. begin
  132. if not(first) then
  133. getopstr:=getopstr+',';
  134. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  135. first:=false;
  136. end;
  137. getopstr:=getopstr+'}';
  138. end;
  139. top_ref:
  140. if o.ref^.refaddr=addr_full then
  141. begin
  142. hs:=o.ref^.symbol.name;
  143. if o.ref^.offset>0 then
  144. hs:=hs+'+'+tostr(o.ref^.offset)
  145. else
  146. if o.ref^.offset<0 then
  147. hs:=hs+tostr(o.ref^.offset);
  148. getopstr:=hs;
  149. end
  150. else
  151. getopstr:=getreferencestring(o.ref^);
  152. else
  153. internalerror(2002070604);
  154. end;
  155. end;
  156. Procedure TArmInstrWriter.WriteInstruction(hp : tai);
  157. var op: TAsmOp;
  158. s: string;
  159. i: byte;
  160. sep: string[3];
  161. begin
  162. op:=taicpu(hp).opcode;
  163. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  164. if taicpu(hp).ops<>0 then
  165. begin
  166. sep:=#9;
  167. for i:=0 to taicpu(hp).ops-1 do
  168. begin
  169. // debug code
  170. // writeln(s);
  171. // writeln(taicpu(hp).fileinfo.line);
  172. { LDM and STM use references as first operand but they are written like a register }
  173. if (i=0) and (op in [A_LDM,A_STM]) then
  174. begin
  175. case taicpu(hp).oper[0]^.typ of
  176. top_ref:
  177. begin
  178. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  179. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  180. s:=s+'!';
  181. end;
  182. top_reg:
  183. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  184. else
  185. internalerror(200311292);
  186. end;
  187. end
  188. { register count of SFM and LFM is written without # }
  189. else if (i=1) and (op in [A_SFM,A_LFM]) then
  190. begin
  191. case taicpu(hp).oper[1]^.typ of
  192. top_const:
  193. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  194. else
  195. internalerror(200311292);
  196. end;
  197. end
  198. else
  199. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  200. sep:=',';
  201. end;
  202. end;
  203. owner.AsmWriteLn(s);
  204. end;
  205. const
  206. as_arm_gas_info : tasminfo =
  207. (
  208. id : as_gas;
  209. idtxt : 'AS';
  210. asmbin : 'as';
  211. asmcmd : '-o $OBJ $ASM';
  212. supported_target : system_any;
  213. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  214. labelprefix : '.L';
  215. comment : '# ';
  216. );
  217. begin
  218. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  219. end.