2
0

ag68kgas.pas 14 KB

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