agcpugas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for m68k GAS syntax
  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 an asmoutput class for i386 AT&T syntax
  19. }
  20. unit agcpugas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  27. type
  28. TM68kAssembler=class(TGNUassembler)
  29. public
  30. procedure WriteInstruction(hp: tai);override;
  31. end;
  32. const
  33. gas_op2str:op2strtable=
  34. { warning: CPU32 opcodes are not fully compatible with the MC68020. }
  35. { 68000 only opcodes }
  36. ('abcd',
  37. 'add','adda','addi','addq','addx','and','andi',
  38. 'asl','asr','bcc','bcs','beq','bge','bgt','bhi',
  39. 'ble','bls','blt','bmi','bne','bpl','bvc','bvs',
  40. 'bchg','bclr','bra','bset','bsr','btst','chk',
  41. 'clr','cmp','cmpa','cmpi','cmpm','dbcc','dbcs','dbeq','dbge',
  42. 'dbgt','dbhi','dble','dbls','dblt','dbmi','dbne','dbra',
  43. 'dbpl','dbt','dbvc','dbvs','dbf','divs','divu',
  44. 'eor','eori','exg','illegal','ext','jmp','jsr',
  45. 'lea','link','lsl','lsr','move','movea','movei','moveq',
  46. 'movem','movep','muls','mulu','nbcd','neg','negx',
  47. 'nop','not','or','ori','pea','rol','ror','roxl',
  48. 'roxr','rtr','rts','sbcd','scc','scs','seq','sge',
  49. 'sgt','shi','sle','sls','slt','smi','sne',
  50. 'spl','st','svc','svs','sf','sub','suba','subi','subq',
  51. 'subx','swap','tas','trap','trapv','tst','unlk',
  52. 'rte','reset','stop',
  53. { mc68010 instructions }
  54. 'bkpt','movec','moves','rtd',
  55. { mc68020 instructions }
  56. 'bfchg','bfclr','bfexts','bfextu','bfffo',
  57. 'bfins','bfset','bftst','callm','cas','cas2',
  58. 'chk2','cmp2','divsl','divul','extb','pack','rtm',
  59. 'trapcc','tracs','trapeq','trapf','trapge','trapgt',
  60. 'traphi','traple','trapls','traplt','trapmi','trapne',
  61. 'trappl','trapt','trapvc','trapvs','unpk',
  62. { fpu processor instructions - directly supported only. }
  63. { ieee aware and misc. condition codes not supported }
  64. 'fabs','fadd',
  65. 'fbeq','fbne','fbngt','fbgt','fbge','fbnge',
  66. 'fblt','fbnlt','fble','fbgl','fbngl','fbgle','fbngle',
  67. 'fdbeq','fdbne','fdbgt','fdbngt','fdbge','fdbnge',
  68. 'fdblt','fdbnlt','fdble','fdbgl','fdbngl','fdbgle','fdbngle',
  69. 'fseq','fsne','fsgt','fsngt','fsge','fsnge',
  70. 'fslt','fsnlt','fsle','fsgl','fsngl','fsgle','fsngle',
  71. 'fcmp','fdiv','fmove','fmovem',
  72. 'fmul','fneg','fnop','fsqrt','fsub','fsgldiv',
  73. 'fsflmul','ftst',
  74. 'ftrapeq','ftrapne','ftrapgt','ftrapngt','ftrapge','ftrapnge',
  75. 'ftraplt','ftrapnlt','ftraple','ftrapgl','ftrapngl','ftrapgle','ftrapngle',
  76. { protected instructions }
  77. 'cprestore','cpsave',
  78. { fpu unit protected instructions }
  79. { and 68030/68851 common mmu instructions }
  80. { (this may include 68040 mmu instructions) }
  81. 'frestore','fsave','pflush','pflusha','pload','pmove','ptest',
  82. { useful for assembly language output }
  83. 'label','none','db','s','b','fb');
  84. gas_opsize2str : array[topsize] of string[2] =
  85. ('','.b','.w','.l','.s','.d','.x',''
  86. );
  87. gas_reg2str : reg2strtable =
  88. ('', '%d0','%d1','%d2','%d3','%d4','%d5','%d6','%d7',
  89. '%a0','%a1','%a2','%a3','%a4','%a5','%a6','%sp',
  90. '-(%sp)','(%sp)+',
  91. '%ccr','%fp0','%fp1','%fp2','%fp3','%fp4','%fp5',
  92. '%fp6','%fp7','%fpcr','%sr','%ssp','%dfc',
  93. '%sfc','%vbr','%fpsr');
  94. implementation
  95. uses
  96. cutils,systems,
  97. cgbase,
  98. verbose,itcpugas;
  99. function getreferencestring(var ref : treference) : string;
  100. var
  101. s,basestr,indexstr : string;
  102. begin
  103. s:='';
  104. with ref do
  105. begin
  106. basestr:=gas_regname(base);
  107. indexstr:=gas_regname(index);
  108. if assigned(symbol) then
  109. s:=s+symbol.name;
  110. if offset<0 then s:=s+tostr(offset)
  111. else if (offset>0) then
  112. begin
  113. if (symbol=nil) then s:=tostr(offset)
  114. else s:=s+'+'+tostr(offset);
  115. end
  116. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  117. s:=s+'0';
  118. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  119. begin
  120. if (scalefactor = 1) or (scalefactor = 0) then
  121. s:=s+'(,'+indexstr+'.l)'
  122. else
  123. s:=s+'(,'+indexstr+'.l*'+tostr(scalefactor)+')'
  124. end
  125. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  126. begin
  127. if (scalefactor = 1) or (scalefactor = 0) then
  128. s:=s+'('+basestr+')+'
  129. else
  130. InternalError(10002);
  131. end
  132. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  133. begin
  134. if (scalefactor = 1) or (scalefactor = 0) then
  135. s:=s+'-('+basestr+')'
  136. else
  137. InternalError(10003);
  138. end
  139. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  140. begin
  141. s:=s+'('+basestr+')'
  142. end
  143. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  144. begin
  145. if (scalefactor = 1) or (scalefactor = 0) then
  146. s:=s+'('+basestr+','+indexstr+'.l)'
  147. else
  148. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  149. end;
  150. end;
  151. getreferencestring:=s;
  152. end;
  153. function getopstr(const o:toper) : string;
  154. var
  155. hs : string;
  156. i : tsuperregister;
  157. begin
  158. case o.typ of
  159. top_reg:
  160. getopstr:=gas_regname(o.reg);
  161. top_ref:
  162. if o.ref^.refaddr=addr_full then
  163. begin
  164. if assigned(o.ref^.symbol) then
  165. hs:='#'+o.ref^.symbol.name
  166. else
  167. hs:='#';
  168. if o.ref^.offset>0 then
  169. hs:=hs+'+'+tostr(o.ref^.offset)
  170. else
  171. if o.ref^.offset<0 then
  172. hs:=hs+tostr(o.ref^.offset)
  173. else
  174. if not(assigned(o.ref^.symbol)) then
  175. hs:=hs+'0';
  176. getopstr:=hs;
  177. end
  178. else
  179. getopstr:=getreferencestring(o.ref^);
  180. top_reglist:
  181. begin
  182. hs:='';
  183. for i:=RS_D0 to RS_D7 do
  184. begin
  185. if i in o.regset^ then
  186. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  187. end;
  188. for i:=RS_A0 to RS_SP do
  189. begin
  190. if i in o.regset^ then
  191. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  192. end;
  193. delete(hs,length(hs),1);
  194. getopstr := hs;
  195. end;
  196. top_const:
  197. getopstr:='#'+tostr(longint(o.val));
  198. else internalerror(200405021);
  199. end;
  200. end;
  201. function getopstr_jmp(const o:toper) : string;
  202. var
  203. hs : string;
  204. begin
  205. case o.typ of
  206. top_reg:
  207. getopstr_jmp:=gas_regname(o.reg);
  208. top_ref:
  209. if o.ref^.refaddr=addr_no then
  210. getopstr_jmp:=getreferencestring(o.ref^)
  211. else
  212. begin
  213. if assigned(o.ref^.symbol) then
  214. hs:=o.ref^.symbol.name
  215. else
  216. hs:='';
  217. if o.ref^.offset>0 then
  218. hs:=hs+'+'+tostr(o.ref^.offset)
  219. else
  220. if o.ref^.offset<0 then
  221. hs:=hs+tostr(o.ref^.offset)
  222. else
  223. if not(assigned(o.ref^.symbol)) then
  224. hs:=hs+'0';
  225. getopstr_jmp:=hs;
  226. end;
  227. top_const:
  228. getopstr_jmp:=tostr(o.val);
  229. else internalerror(200405022);
  230. end;
  231. end;
  232. {****************************************************************************
  233. TM68kASMOUTPUT
  234. ****************************************************************************}
  235. { returns the opcode string }
  236. function getopcodestring(hp : tai) : string;
  237. var
  238. op : tasmop;
  239. s : string;
  240. begin
  241. op:=taicpu(hp).opcode;
  242. { old versions of GAS don't like PEA.L and LEA.L }
  243. if (op in [
  244. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  245. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  246. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  247. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  248. s:=gas_op2str[op]
  249. else
  250. if op = A_SXX then
  251. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  252. else
  253. if op in [a_dbxx,a_bxx,a_fbxx] then
  254. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  255. else
  256. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  257. getopcodestring:=s;
  258. end;
  259. procedure TM68kAssembler.WriteInstruction(hp: tai);
  260. var
  261. op : tasmop;
  262. s : string;
  263. sep : char;
  264. calljmp : boolean;
  265. i : integer;
  266. begin
  267. if hp.typ <> ait_instruction then exit;
  268. op:=taicpu(hp).opcode;
  269. calljmp:=is_calljmp(op);
  270. { call maybe not translated to call }
  271. s:=#9+getopcodestring(hp);
  272. { process operands }
  273. if taicpu(hp).ops<>0 then
  274. begin
  275. { call and jmp need an extra handling }
  276. { this code is only called if jmp isn't a labeled instruction }
  277. { quick hack to overcome a problem with manglednames=255 chars }
  278. if calljmp then
  279. begin
  280. AsmWrite(s+#9);
  281. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  282. end
  283. else
  284. begin
  285. for i:=0 to taicpu(hp).ops-1 do
  286. begin
  287. if i=0 then
  288. sep:=#9
  289. else
  290. if ((op = A_DIVSL) or
  291. (op = A_DIVUL) or
  292. (op = A_MULU) or
  293. (op = A_MULS) or
  294. (op = A_DIVS) or
  295. (op = A_DIVU)) and (i=1) then
  296. sep:=':'
  297. else
  298. sep:=',';
  299. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  300. end;
  301. end;
  302. end;
  303. AsmWriteLn(s);
  304. end;
  305. {*****************************************************************************
  306. Initialize
  307. *****************************************************************************}
  308. const
  309. as_m68k_as_info : tasminfo =
  310. (
  311. id : as_gas;
  312. idtxt : 'AS';
  313. asmbin : 'as';
  314. asmcmd : '-o $OBJ $ASM';
  315. supported_target : system_any;
  316. outputbinary: false;
  317. allowdirect : true;
  318. needar : true;
  319. labelprefix_only_inside_procedure : false;
  320. labelprefix : '.L';
  321. comment : '# ';
  322. secnames : ('',
  323. '.text','.data','.bss',
  324. '','','','','','',
  325. '.stab','.stabstr','COMMON')
  326. );
  327. initialization
  328. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  329. end.
  330. {
  331. $Log$
  332. Revision 1.13 2004-06-20 08:55:31 florian
  333. * logs truncated
  334. Revision 1.12 2004/06/16 20:07:10 florian
  335. * dwarf branch merged
  336. Revision 1.11 2004/05/01 23:29:01 florian
  337. * continued to fix m68k compiler compilation
  338. Revision 1.10 2004/04/27 15:46:01 florian
  339. * several updates for compilation
  340. Revision 1.9 2004/04/27 15:00:37 florian
  341. - removed offsetfixup reference
  342. Revision 1.8 2004/04/25 21:26:16 florian
  343. * some m68k stuff fixed
  344. }