ag68kgas.pas 12 KB

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