ag68kgas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. {
  2. Copyright (c) 1998-2006 by the Free Pascal development team
  3. This unit implements an asmoutput class for m68k GAS syntax
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ag68kgas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,cpubase,
  22. globals,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,aggas;
  24. type
  25. Tm68kGNUAssembler=class(TGNUassembler)
  26. constructor create(smart: boolean); override;
  27. end;
  28. type
  29. Tm68kInstrWriter=class(TCPUInstrWriter)
  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. ( '',
  37. 'abcd','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','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. {****************************************************************************}
  102. { GNU m68k Assembler writer }
  103. {****************************************************************************}
  104. constructor Tm68kGNUAssembler.create(smart: boolean);
  105. begin
  106. inherited create(smart);
  107. InstrWriter := Tm68kInstrWriter.create(self);
  108. end;
  109. function getreferencestring(var ref : treference) : string;
  110. var
  111. s,basestr,indexstr : string;
  112. begin
  113. s:='';
  114. with ref do
  115. begin
  116. basestr:=gas_regname(base);
  117. indexstr:=gas_regname(index);
  118. if assigned(symbol) then
  119. s:=s+symbol.name;
  120. if offset<0 then s:=s+tostr(offset)
  121. else if (offset>0) then
  122. begin
  123. if (symbol=nil) then s:=tostr(offset)
  124. else s:=s+'+'+tostr(offset);
  125. end
  126. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  127. s:=s+'0';
  128. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  129. begin
  130. if (scalefactor = 1) or (scalefactor = 0) then
  131. s:=s+'(,'+indexstr+'.l)'
  132. else
  133. s:=s+'(,'+indexstr+'.l*'+tostr(scalefactor)+')'
  134. end
  135. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  136. begin
  137. if (scalefactor = 1) or (scalefactor = 0) then
  138. s:=s+'('+basestr+')+'
  139. else
  140. InternalError(10002);
  141. end
  142. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  143. begin
  144. if (scalefactor = 1) or (scalefactor = 0) then
  145. s:=s+'-('+basestr+')'
  146. else
  147. InternalError(10003);
  148. end
  149. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  150. begin
  151. s:=s+'('+basestr+')'
  152. end
  153. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  154. begin
  155. if (scalefactor = 1) or (scalefactor = 0) then
  156. s:=s+'('+basestr+','+indexstr+'.l)'
  157. else
  158. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  159. end;
  160. end;
  161. getreferencestring:=s;
  162. end;
  163. function getopstr(const o:toper) : string;
  164. var
  165. hs : string;
  166. i : tsuperregister;
  167. begin
  168. case o.typ of
  169. top_reg: begin
  170. getopstr:=gas_regname(o.reg);
  171. // writeln('top_reg:',getopstr,'!');
  172. end;
  173. top_ref:
  174. if o.ref^.refaddr=addr_full then
  175. begin
  176. if assigned(o.ref^.symbol) then
  177. hs:=o.ref^.symbol.name
  178. else
  179. hs:='#';
  180. if o.ref^.offset>0 then
  181. hs:=hs+'+'+tostr(o.ref^.offset)
  182. else
  183. if o.ref^.offset<0 then
  184. hs:=hs+tostr(o.ref^.offset)
  185. else
  186. if not(assigned(o.ref^.symbol)) then
  187. hs:=hs+'0';
  188. getopstr:=hs;
  189. end
  190. else
  191. getopstr:=getreferencestring(o.ref^);
  192. top_regset:
  193. begin
  194. hs:='';
  195. for i:=RS_D0 to RS_D7 do
  196. begin
  197. if i in o.regset^ then
  198. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  199. end;
  200. for i:=RS_A0 to RS_SP do
  201. begin
  202. if i in o.regset^ then
  203. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  204. end;
  205. delete(hs,length(hs),1);
  206. getopstr := hs;
  207. end;
  208. top_const:
  209. getopstr:='#'+tostr(longint(o.val));
  210. else internalerror(200405021);
  211. end;
  212. end;
  213. function getopstr_jmp(const o:toper) : string;
  214. var
  215. hs : string;
  216. begin
  217. case o.typ of
  218. top_reg:
  219. getopstr_jmp:=gas_regname(o.reg);
  220. top_ref:
  221. if o.ref^.refaddr=addr_no then
  222. getopstr_jmp:=getreferencestring(o.ref^)
  223. else
  224. begin
  225. if assigned(o.ref^.symbol) then
  226. hs:=o.ref^.symbol.name
  227. else
  228. hs:='';
  229. if o.ref^.offset>0 then
  230. hs:=hs+'+'+tostr(o.ref^.offset)
  231. else
  232. if o.ref^.offset<0 then
  233. hs:=hs+tostr(o.ref^.offset)
  234. else
  235. if not(assigned(o.ref^.symbol)) then
  236. hs:=hs+'0';
  237. getopstr_jmp:=hs;
  238. end;
  239. top_const:
  240. getopstr_jmp:=tostr(o.val);
  241. else internalerror(200405022);
  242. end;
  243. end;
  244. {****************************************************************************
  245. TM68kASMOUTPUT
  246. ****************************************************************************}
  247. { returns the opcode string }
  248. function getopcodestring(hp : tai) : string;
  249. var
  250. op : tasmop;
  251. s : string;
  252. begin
  253. op:=taicpu(hp).opcode;
  254. { old versions of GAS don't like PEA.L and LEA.L }
  255. if (op in [
  256. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  257. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  258. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  259. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  260. s:=gas_op2str[op]
  261. else
  262. if op = A_SXX then
  263. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  264. else
  265. { size of DBRA is always WORD, doesn't need opsize (KB) }
  266. if op = A_DBRA then
  267. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  268. else
  269. if op in [a_dbxx,a_bxx,a_fbxx] then
  270. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  271. else
  272. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  273. if op = A_FMOVE then begin
  274. writeln('fmove! opsize:',dword(taicpu(hp).opsize));
  275. end;
  276. getopcodestring:=s;
  277. end;
  278. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  279. var
  280. op : tasmop;
  281. s : string;
  282. sep : char;
  283. calljmp : boolean;
  284. i : integer;
  285. begin
  286. if hp.typ <> ait_instruction then exit;
  287. op:=taicpu(hp).opcode;
  288. calljmp:=is_calljmp(op);
  289. { call maybe not translated to call }
  290. s:=#9+getopcodestring(hp);
  291. { process operands }
  292. if taicpu(hp).ops<>0 then
  293. begin
  294. { call and jmp need an extra handling }
  295. { this code is only called if jmp isn't a labeled instruction }
  296. { quick hack to overcome a problem with manglednames=255 chars }
  297. if calljmp then
  298. begin
  299. owner.AsmWrite(s+#9);
  300. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  301. { dbcc dx,<sym> has two operands! (KB) }
  302. if (taicpu(hp).ops>1) then
  303. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  304. if (taicpu(hp).ops>2) then
  305. internalerror(2006120501);
  306. end
  307. else
  308. begin
  309. for i:=0 to taicpu(hp).ops-1 do
  310. begin
  311. if i=0 then
  312. sep:=#9
  313. else
  314. if ((op = A_DIVSL) or
  315. (op = A_DIVUL) or
  316. (op = A_MULU) or
  317. (op = A_MULS) or
  318. (op = A_DIVS) or
  319. (op = A_DIVU)) and (i=2) then
  320. begin
  321. sep:=':'
  322. end else
  323. sep:=',';
  324. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  325. end;
  326. end;
  327. end;
  328. owner.AsmWriteLn(s);
  329. end;
  330. {*****************************************************************************
  331. Initialize
  332. *****************************************************************************}
  333. const
  334. as_m68k_as_info : tasminfo =
  335. (
  336. id : as_gas;
  337. idtxt : 'AS';
  338. asmbin : 'as';
  339. asmcmd : '-o $OBJ $ASM';
  340. supported_target : system_any;
  341. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  342. labelprefix : '.L';
  343. comment : '# ';
  344. );
  345. initialization
  346. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  347. end.