agarmgas.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. var
  33. gas_reg2str : reg2strtable;
  34. const
  35. gas_shiftmode2str : array[tshiftmode] of string[3] = (
  36. '','lsl','lsr','asr','ror','rrx');
  37. function gas_regnum_search(const s:string):Tnewregister;
  38. function gas_regname(const r:Tnewregister):string;
  39. implementation
  40. uses
  41. cutils,globals,verbose,
  42. systems,
  43. assemble,
  44. aasmcpu;
  45. const
  46. as_arm_gas_info : tasminfo =
  47. (
  48. id : as_gas;
  49. idtxt : 'AS';
  50. asmbin : 'as';
  51. asmcmd : '-o $OBJ $ASM';
  52. supported_target : system_any;
  53. outputbinary: false;
  54. allowdirect : true;
  55. needar : true;
  56. labelprefix_only_inside_procedure : false;
  57. labelprefix : '.L';
  58. comment : '# ';
  59. secnames : ('',
  60. '.text','.data','.text',
  61. '','','','','','',
  62. '.stab','.stabstr','COMMON')
  63. );
  64. function getreferencestring(var ref : treference) : string;
  65. var
  66. s : string;
  67. nobase,noindex : boolean;
  68. begin
  69. with ref do
  70. begin
  71. inc(offset,offsetfixup);
  72. noindex:=(index.enum=R_NO) or ((index.enum=R_INTREGISTER) and (index.number=NR_NO));
  73. {$ifdef extdebug}
  74. nobase:=(base.enum=R_NO) or ((base.enum=R_INTREGISTER) and (base.number=NR_NO));
  75. //!!!! if nobase then
  76. //!!!! internalerror(200308292);
  77. // !!! if (not(noindex) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  78. // !!! internalerror(200308293);
  79. {$endif extdebug}
  80. if assigned(symbol) then
  81. begin
  82. // if (base.enum<>R_NO) and not(is_pc(base)) then
  83. // internalerror(200309011);
  84. s:=symbol.name;
  85. if offset<0 then
  86. s:=s+tostr(offset)
  87. else if offset>0 then
  88. s:=s+'+'+tostr(offset);
  89. end
  90. else
  91. begin
  92. if base.enum=R_INTREGISTER then
  93. s:='['+gas_regname(base.number)
  94. else
  95. s:='['+gas_reg2str[base.enum];
  96. if addressmode=AM_POSTINDEXED then
  97. s:=s+']';
  98. if not(noindex) then
  99. begin
  100. if signindex<0 then
  101. s:=s+', -'
  102. else
  103. s:=s+', ';
  104. if index.enum=R_INTREGISTER then
  105. s:=s+gas_regname(index.number)
  106. else
  107. s:=s+gas_reg2str[index.enum];
  108. if shiftmode<>SM_None then
  109. s:=s+' ,'+gas_shiftmode2str[shiftmode]+' #'+tostr(shiftimm);
  110. end;
  111. case addressmode of
  112. AM_OFFSET:
  113. s:=s+']';
  114. AM_PREINDEXED:
  115. s:=s+']!';
  116. end;
  117. end;
  118. end;
  119. getreferencestring:=s;
  120. end;
  121. function getopstr_jmp(const o:toper) : string;
  122. var
  123. hs : string;
  124. begin
  125. case o.typ of
  126. top_reg :
  127. begin
  128. if (o.reg.enum < R_R0) or (o.reg.enum > lastreg) then
  129. internalerror(200303121);
  130. getopstr_jmp:=std_reg2str[o.reg.enum];
  131. end;
  132. top_shifterop:
  133. begin
  134. end;
  135. { no top_ref jumping for powerpc }
  136. top_const :
  137. getopstr_jmp:='#'+tostr(o.val);
  138. top_symbol :
  139. begin
  140. hs:=o.sym.name;
  141. if o.symofs>0 then
  142. hs:=hs+'+'+tostr(o.symofs)
  143. else
  144. if o.symofs<0 then
  145. hs:=hs+tostr(o.symofs);
  146. getopstr_jmp:=hs;
  147. end;
  148. top_none:
  149. getopstr_jmp:='';
  150. else
  151. {$ifndef testing}
  152. internalerror(2002070603);
  153. {$else testing}
  154. begin
  155. writeln('internalerror 10001');
  156. halt(1);
  157. end;
  158. {$endif testing}
  159. end;
  160. end;
  161. const
  162. shiftmode2str: array[tshiftmode] of string[3] = ('','lsl','lsr','asr','ror','rrx');
  163. function getopstr(const o:toper) : string;
  164. var
  165. hs : string;
  166. first : boolean;
  167. r : tnewregister;
  168. begin
  169. case o.typ of
  170. top_reg:
  171. begin
  172. if o.reg.enum=R_INTREGISTER then
  173. getopstr:=gas_regname(o.reg.number)
  174. else
  175. getopstr:=gas_reg2str[o.reg.enum];
  176. end;
  177. top_shifterop:
  178. begin
  179. if (o.shifterop^.rs.enum<>R_NO) and (o.shifterop^.shiftimm=0) then
  180. begin
  181. if o.shifterop^.rs.enum=R_INTREGISTER then
  182. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_regname(o.shifterop^.rs.number)
  183. else
  184. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' '+gas_reg2str[o.shifterop^.rs.enum];
  185. end
  186. else if (o.shifterop^.rs.enum=R_NO) then
  187. getopstr:=shiftmode2str[o.shifterop^.shiftmode]+' #'+tostr(o.shifterop^.shiftimm)
  188. else internalerror(200308282);
  189. end;
  190. top_const:
  191. getopstr:='#'+tostr(longint(o.val));
  192. top_regset:
  193. begin
  194. getopstr:='{';
  195. first:=true;
  196. for r:=RS_R0 to RS_R15 do
  197. if r in o.regset then
  198. begin
  199. if not(first) then
  200. getopstr:=getopstr+',';
  201. getopstr:=getopstr+'r'+tostr(r-RS_R0);
  202. first:=false;
  203. end;
  204. getopstr:=getopstr+'}';
  205. end;
  206. top_ref:
  207. getopstr:=getreferencestring(o.ref^);
  208. top_symbol:
  209. begin
  210. hs:=o.sym.name;
  211. if o.symofs>0 then
  212. hs:=hs+'+'+tostr(o.symofs)
  213. else
  214. if o.symofs<0 then
  215. hs:=hs+tostr(o.symofs);
  216. getopstr:=hs;
  217. end;
  218. else
  219. {$ifndef testing}
  220. internalerror(2002070604);
  221. {$else testing}
  222. begin
  223. writeln('internalerror 10001');
  224. halt(1);
  225. end;
  226. {$endif testing}
  227. end;
  228. end;
  229. Procedure TARMGNUAssembler.WriteInstruction(hp : tai);
  230. var op: TAsmOp;
  231. s: string;
  232. i: byte;
  233. sep: string[3];
  234. begin
  235. op:=taicpu(hp).opcode;
  236. {
  237. if is_calljmp(op) then
  238. begin
  239. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  240. case op of
  241. A_B,A_BA,A_BL,A_BLA:
  242. s:=#9+op2str[op]+#9;
  243. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  244. s:=#9+op2str[op]
  245. else
  246. s:=cond2str(op,taicpu(hp).condition)+',';
  247. end;
  248. if (taicpu(hp).oper[0].typ <> top_none) then
  249. s:=s+getopstr_jmp(taicpu(hp).oper[0]);
  250. end
  251. else
  252. }
  253. { process operands }
  254. begin
  255. s:=#9+std_op2str[op]+cond2str[taicpu(hp).condition]+oppostfix2str[taicpu(hp).oppostfix];
  256. if taicpu(hp).ops<>0 then
  257. begin
  258. {
  259. if not is_calljmp(op) then
  260. sep:=','
  261. else
  262. }
  263. sep:=#9;
  264. for i:=0 to taicpu(hp).ops-1 do
  265. begin
  266. // debug code
  267. // writeln(s);
  268. // writeln(taicpu(hp).fileinfo.line);
  269. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  270. sep:=',';
  271. end;
  272. end;
  273. end;
  274. AsmWriteLn(s);
  275. end;
  276. function gas_regnum_search(const s:string):Tnewregister;
  277. begin
  278. end;
  279. function gas_regname(const r:Tnewregister):string;
  280. var s:Tsuperregister;
  281. begin
  282. s:=r shr 8;
  283. if s in [RS_R0..RS_R15] then
  284. gas_regname:='r'+tostr(s-RS_R0)
  285. else
  286. begin
  287. {Generate a systematic name.}
  288. gas_regname:='reg'+tostr(s)+'d';
  289. end;
  290. end;
  291. begin
  292. RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
  293. gas_reg2str:=std_reg2str;
  294. end.
  295. {
  296. $Log$
  297. Revision 1.8 2003-09-03 19:10:30 florian
  298. * initial revision of new register naming
  299. Revision 1.7 2003/09/01 15:11:16 florian
  300. * fixed reference handling
  301. * fixed operand postfix for floating point instructions
  302. * fixed wrong shifter constant handling
  303. Revision 1.6 2003/08/29 21:36:28 florian
  304. * fixed procedure entry/exit code
  305. * started to fix reference handling
  306. Revision 1.5 2003/08/28 13:26:10 florian
  307. * another couple of arm fixes
  308. Revision 1.4 2003/08/28 00:05:29 florian
  309. * today's arm patches
  310. Revision 1.3 2003/08/24 12:27:26 florian
  311. * continued to work on the arm port
  312. Revision 1.2 2003/08/20 15:50:12 florian
  313. * more arm stuff
  314. Revision 1.1 2003/08/16 13:23:01 florian
  315. * several arm related stuff fixed
  316. }