agarmgas.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. $Id$
  3. Copyright (c) 2003 by Florian Klaempfl
  4. This unit implements an asm for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the GNU Assembler writer for the ARM
  19. }
  20. unit agarmgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmtai,
  25. aggas,
  26. cpubase;
  27. type
  28. PARMGNUAssembler=^TARMGNUAssembler;
  29. TARMGNUAssembler=class(TGNUassembler)
  30. procedure WriteInstruction(hp : tai);override;
  31. end;
  32. const
  33. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  34. '','lsl','lsr','asr','ror','rrx');
  35. implementation
  36. uses
  37. cutils,globals,verbose,
  38. systems,
  39. assemble,
  40. aasmcpu,
  41. itarmgas;
  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. outputbinary: false;
  51. allowdirect : true;
  52. needar : true;
  53. labelprefix_only_inside_procedure : false;
  54. labelprefix : '.L';
  55. comment : '# ';
  56. secnames : ('',
  57. '.text','.data','.text',
  58. '','','','','','',
  59. '.stab','.stabstr','COMMON')
  60. );
  61. function getreferencestring(var ref : treference) : string;
  62. var
  63. s : string;
  64. begin
  65. with ref do
  66. begin
  67. inc(offset,offsetfixup);
  68. {$ifdef extdebug}
  69. // if base=NR_NO then
  70. // internalerror(200308292);
  71. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  72. // internalerror(200308293);
  73. {$endif extdebug}
  74. if assigned(symbol) then
  75. begin
  76. if (base<>NR_NO) and not(is_pc(base)) then
  77. internalerror(200309011);
  78. s:=symbol.name;
  79. if offset<0 then
  80. s:=s+tostr(offset)
  81. else if offset>0 then
  82. s:=s+'+'+tostr(offset);
  83. end
  84. else
  85. begin
  86. s:='['+gas_regname(base);
  87. if addressmode=AM_POSTINDEXED then
  88. s:=s+']';
  89. if index<>NR_NO then
  90. begin
  91. if signindex<0 then
  92. s:=s+', -'
  93. else
  94. s:=s+', ';
  95. s:=s+gas_regname(index);
  96. if shiftmode<>SM_None then
  97. s:=s+' ,'+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  98. end
  99. else if offset<>0 then
  100. s:=s+', #'+tostr(offset);
  101. case addressmode of
  102. AM_OFFSET:
  103. s:=s+']';
  104. AM_PREINDEXED:
  105. s:=s+']!';
  106. end;
  107. end;
  108. end;
  109. getreferencestring:=s;
  110. end;
  111. const
  112. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  113. function getopstr(const o:toper) : string;
  114. var
  115. hs : string;
  116. first : boolean;
  117. r : tregister;
  118. begin
  119. case o.typ of
  120. top_reg:
  121. getopstr:=gas_regname(o.reg);
  122. top_shifterop:
  123. begin
  124. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  125. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  126. else if (o.shifterop^.rs=NR_NO) then
  127. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  128. else internalerror(200308282);
  129. end;
  130. top_const:
  131. getopstr:='#'+tostr(longint(o.val));
  132. top_regset:
  133. begin
  134. getopstr:='{';
  135. first:=true;
  136. for r:=RS_R0 to RS_R15 do
  137. if r in o.regset^ then
  138. begin
  139. if not(first) then
  140. getopstr:=getopstr+',';
  141. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  142. first:=false;
  143. end;
  144. getopstr:=getopstr+'}';
  145. end;
  146. top_ref:
  147. getopstr:=getreferencestring(o.ref^);
  148. top_symbol:
  149. begin
  150. hs:=o.sym.name;
  151. if o.symofs>0 then
  152. hs:=hs+'+'+tostr(o.symofs)
  153. else
  154. if o.symofs<0 then
  155. hs:=hs+tostr(o.symofs);
  156. getopstr:=hs;
  157. end;
  158. else
  159. internalerror(2002070604);
  160. end;
  161. end;
  162. Procedure TARMGNUAssembler.WriteInstruction(hp : tai);
  163. var op: TAsmOp;
  164. s: string;
  165. i: byte;
  166. sep: string[3];
  167. begin
  168. op:=taicpu(hp).opcode;
  169. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  170. if taicpu(hp).ops<>0 then
  171. begin
  172. sep:=#9;
  173. for i:=0 to taicpu(hp).ops-1 do
  174. begin
  175. // debug code
  176. // writeln(s);
  177. // writeln(taicpu(hp).fileinfo.line);
  178. { LDM and STM use references as first operand but they are written like a register }
  179. if (i=0) and (op in [A_LDM,A_STM]) then
  180. begin
  181. s:=s+sep+gas_regname(taicpu(hp).oper[0].ref^.index);
  182. if taicpu(hp).oper[0].ref^.addressmode=AM_PREINDEXED then
  183. s:=s+'!';
  184. end
  185. else
  186. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  187. sep:=',';
  188. end;
  189. end;
  190. AsmWriteLn(s);
  191. end;
  192. begin
  193. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  194. end.
  195. {
  196. $Log$
  197. Revision 1.12 2003-11-02 14:30:03 florian
  198. * fixed ARM for new reg. allocation scheme
  199. Revision 1.11 2003/09/06 11:21:49 florian
  200. * fixed stm and ldm to be usable with preindex operand
  201. Revision 1.10 2003/09/05 23:57:01 florian
  202. * arm is working again as before the new register naming scheme was implemented
  203. Revision 1.9 2003/09/04 00:15:29 florian
  204. * first bunch of adaptions of arm compiler for new register type
  205. Revision 1.8 2003/09/03 19:10:30 florian
  206. * initial revision of new register naming
  207. Revision 1.7 2003/09/01 15:11:16 florian
  208. * fixed reference handling
  209. * fixed operand postfix for floating point instructions
  210. * fixed wrong shifter constant handling
  211. Revision 1.6 2003/08/29 21:36:28 florian
  212. * fixed procedure entry/exit code
  213. * started to fix reference handling
  214. Revision 1.5 2003/08/28 13:26:10 florian
  215. * another couple of arm fixes
  216. Revision 1.4 2003/08/28 00:05:29 florian
  217. * today's arm patches
  218. Revision 1.3 2003/08/24 12:27:26 florian
  219. * continued to work on the arm port
  220. Revision 1.2 2003/08/20 15:50:12 florian
  221. * more arm stuff
  222. Revision 1.1 2003/08/16 13:23:01 florian
  223. * several arm related stuff fixed
  224. }