agarmgas.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. cgbase;
  43. const
  44. as_arm_gas_info : tasminfo =
  45. (
  46. id : as_gas;
  47. idtxt : 'AS';
  48. asmbin : 'as';
  49. asmcmd : '-o $OBJ $ASM';
  50. supported_target : system_any;
  51. outputbinary: false;
  52. allowdirect : true;
  53. needar : true;
  54. labelprefix_only_inside_procedure : false;
  55. labelprefix : '.L';
  56. comment : '# ';
  57. secnames : ('',
  58. '.text','.data','.text',
  59. '','','','','','',
  60. '.stab','.stabstr','COMMON')
  61. );
  62. function getreferencestring(var ref : treference) : string;
  63. var
  64. s : string;
  65. begin
  66. with ref do
  67. begin
  68. inc(offset,offsetfixup);
  69. {$ifdef extdebug}
  70. // if base=NR_NO then
  71. // internalerror(200308292);
  72. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  73. // internalerror(200308293);
  74. {$endif extdebug}
  75. if assigned(symbol) then
  76. begin
  77. if (base<>NR_NO) and not(is_pc(base)) then
  78. internalerror(200309011);
  79. s:=symbol.name;
  80. if offset<0 then
  81. s:=s+tostr(offset)
  82. else if offset>0 then
  83. s:=s+'+'+tostr(offset);
  84. end
  85. else
  86. begin
  87. s:='['+gas_regname(base);
  88. if addressmode=AM_POSTINDEXED then
  89. s:=s+']';
  90. if index<>NR_NO then
  91. begin
  92. if signindex<0 then
  93. s:=s+', -'
  94. else
  95. s:=s+', ';
  96. s:=s+gas_regname(index);
  97. if shiftmode<>SM_None then
  98. s:=s+' ,'+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  99. end
  100. else if offset<>0 then
  101. s:=s+', #'+tostr(offset);
  102. case addressmode of
  103. AM_OFFSET:
  104. s:=s+']';
  105. AM_PREINDEXED:
  106. s:=s+']!';
  107. end;
  108. end;
  109. end;
  110. getreferencestring:=s;
  111. end;
  112. const
  113. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  114. function getopstr(const o:toper) : string;
  115. var
  116. hs : string;
  117. first : boolean;
  118. r : tsuperregister;
  119. begin
  120. case o.typ of
  121. top_reg:
  122. getopstr:=gas_regname(o.reg);
  123. top_shifterop:
  124. begin
  125. if (o.shifterop^.rs<>NR_NO) and (o.shifterop^.shiftimm=0) then
  126. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs)
  127. else if (o.shifterop^.rs=NR_NO) then
  128. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  129. else internalerror(200308282);
  130. end;
  131. top_const:
  132. getopstr:='#'+tostr(longint(o.val));
  133. top_regset:
  134. begin
  135. getopstr:='{';
  136. first:=true;
  137. for r:=RS_R0 to RS_R15 do
  138. if r in o.regset^ then
  139. begin
  140. if not(first) then
  141. getopstr:=getopstr+',';
  142. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  143. first:=false;
  144. end;
  145. getopstr:=getopstr+'}';
  146. end;
  147. top_ref:
  148. getopstr:=getreferencestring(o.ref^);
  149. top_symbol:
  150. begin
  151. hs:=o.sym.name;
  152. if o.symofs>0 then
  153. hs:=hs+'+'+tostr(o.symofs)
  154. else
  155. if o.symofs<0 then
  156. hs:=hs+tostr(o.symofs);
  157. getopstr:=hs;
  158. end;
  159. else
  160. internalerror(2002070604);
  161. end;
  162. end;
  163. Procedure TARMGNUAssembler.WriteInstruction(hp : tai);
  164. var op: TAsmOp;
  165. s: string;
  166. i: byte;
  167. sep: string[3];
  168. begin
  169. op:=taicpu(hp).opcode;
  170. s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  171. if taicpu(hp).ops<>0 then
  172. begin
  173. sep:=#9;
  174. for i:=0 to taicpu(hp).ops-1 do
  175. begin
  176. // debug code
  177. // writeln(s);
  178. // writeln(taicpu(hp).fileinfo.line);
  179. { LDM and STM use references as first operand but they are written like a register }
  180. if (i=0) and (op in [A_LDM,A_STM]) then
  181. begin
  182. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  183. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  184. s:=s+'!';
  185. end
  186. else
  187. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  188. sep:=',';
  189. end;
  190. end;
  191. AsmWriteLn(s);
  192. end;
  193. begin
  194. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  195. end.
  196. {
  197. $Log$
  198. Revision 1.13 2003-11-07 15:58:32 florian
  199. * Florian's culmutative nr. 1; contains:
  200. - invalid calling conventions for a certain cpu are rejected
  201. - arm softfloat calling conventions
  202. - -Sp for cpu dependend code generation
  203. - several arm fixes
  204. - remaining code for value open array paras on heap
  205. Revision 1.12 2003/11/02 14:30:03 florian
  206. * fixed ARM for new reg. allocation scheme
  207. Revision 1.11 2003/09/06 11:21:49 florian
  208. * fixed stm and ldm to be usable with preindex operand
  209. Revision 1.10 2003/09/05 23:57:01 florian
  210. * arm is working again as before the new register naming scheme was implemented
  211. Revision 1.9 2003/09/04 00:15:29 florian
  212. * first bunch of adaptions of arm compiler for new register type
  213. Revision 1.8 2003/09/03 19:10:30 florian
  214. * initial revision of new register naming
  215. Revision 1.7 2003/09/01 15:11:16 florian
  216. * fixed reference handling
  217. * fixed operand postfix for floating point instructions
  218. * fixed wrong shifter constant handling
  219. Revision 1.6 2003/08/29 21:36:28 florian
  220. * fixed procedure entry/exit code
  221. * started to fix reference handling
  222. Revision 1.5 2003/08/28 13:26:10 florian
  223. * another couple of arm fixes
  224. Revision 1.4 2003/08/28 00:05:29 florian
  225. * today's arm patches
  226. Revision 1.3 2003/08/24 12:27:26 florian
  227. * continued to work on the arm port
  228. Revision 1.2 2003/08/20 15:50:12 florian
  229. * more arm stuff
  230. Revision 1.1 2003/08/16 13:23:01 florian
  231. * several arm related stuff fixed
  232. }