agarmgas.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. itcpugas,
  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. {$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 : tsuperregister;
  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. if o.ref^.refaddr=addr_full then
  148. begin
  149. hs:=o.ref^.symbol.name;
  150. if o.ref^.offset>0 then
  151. hs:=hs+'+'+tostr(o.ref^.offset)
  152. else
  153. if o.ref^.offset<0 then
  154. hs:=hs+tostr(o.ref^.offset);
  155. getopstr:=hs;
  156. end
  157. else
  158. getopstr:=getreferencestring(o.ref^);
  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. case taicpu(hp).oper[0]^.typ of
  183. top_ref:
  184. begin
  185. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.ref^.index);
  186. if taicpu(hp).oper[0]^.ref^.addressmode=AM_PREINDEXED then
  187. s:=s+'!';
  188. end;
  189. top_reg:
  190. s:=s+sep+gas_regname(taicpu(hp).oper[0]^.reg);
  191. else
  192. internalerror(200311292);
  193. end;
  194. end
  195. { register count of SFM and LFM is written without # }
  196. else if (i=1) and (op in [A_SFM,A_LFM]) then
  197. begin
  198. case taicpu(hp).oper[1]^.typ of
  199. top_const:
  200. s:=s+sep+tostr(taicpu(hp).oper[1]^.val);
  201. else
  202. internalerror(200311292);
  203. end;
  204. end
  205. else
  206. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  207. sep:=',';
  208. end;
  209. end;
  210. AsmWriteLn(s);
  211. end;
  212. begin
  213. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  214. end.
  215. {
  216. $Log$
  217. Revision 1.19 2004-03-29 19:19:35 florian
  218. + arm floating point register saving implemented
  219. * hopefully stabs generation for MacOSX fixed
  220. + some defines for arm added
  221. Revision 1.18 2004/03/06 20:35:19 florian
  222. * fixed arm compilation
  223. * cleaned up code generation for exported linux procedures
  224. Revision 1.17 2003/11/30 19:35:29 florian
  225. * fixed several arm related problems
  226. Revision 1.16 2003/11/29 17:36:56 peter
  227. * fixed is_move
  228. Revision 1.15 2003/11/21 16:29:26 florian
  229. * fixed reading of reg. sets in the arm assembler reader
  230. Revision 1.14 2003/11/17 23:23:47 florian
  231. + first part of arm assembler reader
  232. Revision 1.13 2003/11/07 15:58:32 florian
  233. * Florian's culmutative nr. 1; contains:
  234. - invalid calling conventions for a certain cpu are rejected
  235. - arm softfloat calling conventions
  236. - -Sp for cpu dependend code generation
  237. - several arm fixes
  238. - remaining code for value open array paras on heap
  239. Revision 1.12 2003/11/02 14:30:03 florian
  240. * fixed ARM for new reg. allocation scheme
  241. Revision 1.11 2003/09/06 11:21:49 florian
  242. * fixed stm and ldm to be usable with preindex operand
  243. Revision 1.10 2003/09/05 23:57:01 florian
  244. * arm is working again as before the new register naming scheme was implemented
  245. Revision 1.9 2003/09/04 00:15:29 florian
  246. * first bunch of adaptions of arm compiler for new register type
  247. Revision 1.8 2003/09/03 19:10:30 florian
  248. * initial revision of new register naming
  249. Revision 1.7 2003/09/01 15:11:16 florian
  250. * fixed reference handling
  251. * fixed operand postfix for floating point instructions
  252. * fixed wrong shifter constant handling
  253. Revision 1.6 2003/08/29 21:36:28 florian
  254. * fixed procedure entry/exit code
  255. * started to fix reference handling
  256. Revision 1.5 2003/08/28 13:26:10 florian
  257. * another couple of arm fixes
  258. Revision 1.4 2003/08/28 00:05:29 florian
  259. * today's arm patches
  260. Revision 1.3 2003/08/24 12:27:26 florian
  261. * continued to work on the arm port
  262. Revision 1.2 2003/08/20 15:50:12 florian
  263. * more arm stuff
  264. Revision 1.1 2003/08/16 13:23:01 florian
  265. * several arm related stuff fixed
  266. }