agcpugas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. {
  88. gas_reg2str : treg2strtable =
  89. ('', '%d0','%d1','%d2','%d3','%d4','%d5','%d6','%d7',
  90. '%a0','%a1','%a2','%a3','%a4','%a5','%a6','%sp',
  91. '-(%sp)','(%sp)+',
  92. '%ccr','%fp0','%fp1','%fp2','%fp3','%fp4','%fp5',
  93. '%fp6','%fp7','%fpcr','%sr','%ssp','%dfc',
  94. '%sfc','%vbr','%fpsr');
  95. }
  96. implementation
  97. uses
  98. cutils,systems,
  99. cgbase,cgutils,
  100. verbose,itcpugas;
  101. function getreferencestring(var ref : treference) : string;
  102. var
  103. s,basestr,indexstr : string;
  104. begin
  105. s:='';
  106. with ref do
  107. begin
  108. basestr:=gas_regname(base);
  109. indexstr:=gas_regname(index);
  110. if assigned(symbol) then
  111. s:=s+symbol.name;
  112. if offset<0 then s:=s+tostr(offset)
  113. else if (offset>0) then
  114. begin
  115. if (symbol=nil) then s:=tostr(offset)
  116. else s:=s+'+'+tostr(offset);
  117. end
  118. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  119. s:=s+'0';
  120. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  121. begin
  122. if (scalefactor = 1) or (scalefactor = 0) then
  123. s:=s+'(,'+indexstr+'.l)'
  124. else
  125. s:=s+'(,'+indexstr+'.l*'+tostr(scalefactor)+')'
  126. end
  127. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  128. begin
  129. if (scalefactor = 1) or (scalefactor = 0) then
  130. s:=s+'('+basestr+')+'
  131. else
  132. InternalError(10002);
  133. end
  134. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  135. begin
  136. if (scalefactor = 1) or (scalefactor = 0) then
  137. s:=s+'-('+basestr+')'
  138. else
  139. InternalError(10003);
  140. end
  141. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  142. begin
  143. s:=s+'('+basestr+')'
  144. end
  145. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  146. begin
  147. if (scalefactor = 1) or (scalefactor = 0) then
  148. s:=s+'('+basestr+','+indexstr+'.l)'
  149. else
  150. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  151. end;
  152. end;
  153. getreferencestring:=s;
  154. end;
  155. function getopstr(const o:toper) : string;
  156. var
  157. hs : string;
  158. i : tsuperregister;
  159. begin
  160. case o.typ of
  161. top_reg:
  162. getopstr:=gas_regname(o.reg);
  163. top_ref:
  164. if o.ref^.refaddr=addr_full then
  165. begin
  166. if assigned(o.ref^.symbol) then
  167. hs:='#'+o.ref^.symbol.name
  168. else
  169. hs:='#';
  170. if o.ref^.offset>0 then
  171. hs:=hs+'+'+tostr(o.ref^.offset)
  172. else
  173. if o.ref^.offset<0 then
  174. hs:=hs+tostr(o.ref^.offset)
  175. else
  176. if not(assigned(o.ref^.symbol)) then
  177. hs:=hs+'0';
  178. getopstr:=hs;
  179. end
  180. else
  181. getopstr:=getreferencestring(o.ref^);
  182. top_reglist:
  183. begin
  184. hs:='';
  185. for i:=RS_D0 to RS_D7 do
  186. begin
  187. if i in o.regset^ then
  188. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  189. end;
  190. for i:=RS_A0 to RS_SP do
  191. begin
  192. if i in o.regset^ then
  193. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  194. end;
  195. delete(hs,length(hs),1);
  196. getopstr := hs;
  197. end;
  198. top_const:
  199. getopstr:='#'+tostr(longint(o.val));
  200. else internalerror(200405021);
  201. end;
  202. end;
  203. function getopstr_jmp(const o:toper) : string;
  204. var
  205. hs : string;
  206. begin
  207. case o.typ of
  208. top_reg:
  209. getopstr_jmp:=gas_regname(o.reg);
  210. top_ref:
  211. if o.ref^.refaddr=addr_no then
  212. getopstr_jmp:=getreferencestring(o.ref^)
  213. else
  214. begin
  215. if assigned(o.ref^.symbol) then
  216. hs:=o.ref^.symbol.name
  217. else
  218. hs:='';
  219. if o.ref^.offset>0 then
  220. hs:=hs+'+'+tostr(o.ref^.offset)
  221. else
  222. if o.ref^.offset<0 then
  223. hs:=hs+tostr(o.ref^.offset)
  224. else
  225. if not(assigned(o.ref^.symbol)) then
  226. hs:=hs+'0';
  227. getopstr_jmp:=hs;
  228. end;
  229. top_const:
  230. getopstr_jmp:=tostr(o.val);
  231. else internalerror(200405022);
  232. end;
  233. end;
  234. {****************************************************************************
  235. TM68kASMOUTPUT
  236. ****************************************************************************}
  237. { returns the opcode string }
  238. function getopcodestring(hp : tai) : string;
  239. var
  240. op : tasmop;
  241. s : string;
  242. begin
  243. op:=taicpu(hp).opcode;
  244. { old versions of GAS don't like PEA.L and LEA.L }
  245. if (op in [
  246. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  247. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  248. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  249. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  250. s:=gas_op2str[op]
  251. else
  252. if op = A_SXX then
  253. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  254. else
  255. if op in [a_dbxx,a_bxx,a_fbxx] then
  256. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  257. else
  258. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  259. getopcodestring:=s;
  260. end;
  261. procedure TM68kAssembler.WriteInstruction(hp: tai);
  262. var
  263. op : tasmop;
  264. s : string;
  265. sep : char;
  266. calljmp : boolean;
  267. i : integer;
  268. begin
  269. if hp.typ <> ait_instruction then exit;
  270. op:=taicpu(hp).opcode;
  271. calljmp:=is_calljmp(op);
  272. { call maybe not translated to call }
  273. s:=#9+getopcodestring(hp);
  274. { process operands }
  275. if taicpu(hp).ops<>0 then
  276. begin
  277. { call and jmp need an extra handling }
  278. { this code is only called if jmp isn't a labeled instruction }
  279. { quick hack to overcome a problem with manglednames=255 chars }
  280. if calljmp then
  281. begin
  282. AsmWrite(s+#9);
  283. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  284. end
  285. else
  286. begin
  287. for i:=0 to taicpu(hp).ops-1 do
  288. begin
  289. if i=0 then
  290. sep:=#9
  291. else
  292. if ((op = A_DIVSL) or
  293. (op = A_DIVUL) or
  294. (op = A_MULU) or
  295. (op = A_MULS) or
  296. (op = A_DIVS) or
  297. (op = A_DIVU)) and (i=1) then
  298. sep:=':'
  299. else
  300. sep:=',';
  301. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  302. end;
  303. end;
  304. end;
  305. AsmWriteLn(s);
  306. end;
  307. {*****************************************************************************
  308. Initialize
  309. *****************************************************************************}
  310. const
  311. as_m68k_as_info : tasminfo =
  312. (
  313. id : as_gas;
  314. idtxt : 'AS';
  315. asmbin : 'as';
  316. asmcmd : '-o $OBJ $ASM';
  317. supported_target : system_any;
  318. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  319. labelprefix : '.L';
  320. comment : '# ';
  321. );
  322. initialization
  323. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  324. end.
  325. {
  326. $Log$
  327. Revision 1.14 2005-01-08 04:10:36 karoly
  328. * made m68k to compile again
  329. Revision 1.13 2004/06/20 08:55:31 florian
  330. * logs truncated
  331. Revision 1.12 2004/06/16 20:07:10 florian
  332. * dwarf branch merged
  333. Revision 1.11 2004/05/01 23:29:01 florian
  334. * continued to fix m68k compiler compilation
  335. Revision 1.10 2004/04/27 15:46:01 florian
  336. * several updates for compilation
  337. Revision 1.9 2004/04/27 15:00:37 florian
  338. - removed offsetfixup reference
  339. Revision 1.8 2004/04/25 21:26:16 florian
  340. * some m68k stuff fixed
  341. }