agcpugas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 agcpugas;
  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:
  161. getopstr:=gas_regname(o.reg);
  162. top_ref:
  163. if o.ref^.refaddr=addr_full then
  164. begin
  165. if assigned(o.ref^.symbol) then
  166. hs:='#'+o.ref^.symbol.name
  167. else
  168. hs:='#';
  169. if o.ref^.offset>0 then
  170. hs:=hs+'+'+tostr(o.ref^.offset)
  171. else
  172. if o.ref^.offset<0 then
  173. hs:=hs+tostr(o.ref^.offset)
  174. else
  175. if not(assigned(o.ref^.symbol)) then
  176. hs:=hs+'0';
  177. getopstr:=hs;
  178. end
  179. else
  180. getopstr:=getreferencestring(o.ref^);
  181. top_regset:
  182. begin
  183. hs:='';
  184. for i:=RS_D0 to RS_D7 do
  185. begin
  186. if i in o.regset^ then
  187. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  188. end;
  189. for i:=RS_A0 to RS_SP do
  190. begin
  191. if i in o.regset^ then
  192. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  193. end;
  194. delete(hs,length(hs),1);
  195. getopstr := hs;
  196. end;
  197. top_const:
  198. getopstr:='#'+tostr(longint(o.val));
  199. else internalerror(200405021);
  200. end;
  201. end;
  202. function getopstr_jmp(const o:toper) : string;
  203. var
  204. hs : string;
  205. begin
  206. case o.typ of
  207. top_reg:
  208. getopstr_jmp:=gas_regname(o.reg);
  209. top_ref:
  210. if o.ref^.refaddr=addr_no then
  211. getopstr_jmp:=getreferencestring(o.ref^)
  212. else
  213. begin
  214. if assigned(o.ref^.symbol) then
  215. hs:=o.ref^.symbol.name
  216. else
  217. hs:='';
  218. if o.ref^.offset>0 then
  219. hs:=hs+'+'+tostr(o.ref^.offset)
  220. else
  221. if o.ref^.offset<0 then
  222. hs:=hs+tostr(o.ref^.offset)
  223. else
  224. if not(assigned(o.ref^.symbol)) then
  225. hs:=hs+'0';
  226. getopstr_jmp:=hs;
  227. end;
  228. top_const:
  229. getopstr_jmp:=tostr(o.val);
  230. else internalerror(200405022);
  231. end;
  232. end;
  233. {****************************************************************************
  234. TM68kASMOUTPUT
  235. ****************************************************************************}
  236. { returns the opcode string }
  237. function getopcodestring(hp : tai) : string;
  238. var
  239. op : tasmop;
  240. s : string;
  241. begin
  242. op:=taicpu(hp).opcode;
  243. { old versions of GAS don't like PEA.L and LEA.L }
  244. if (op in [
  245. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  246. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  247. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  248. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  249. s:=gas_op2str[op]
  250. else
  251. if op = A_SXX then
  252. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  253. else
  254. if op in [a_dbxx,a_bxx,a_fbxx] then
  255. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  256. else
  257. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  258. getopcodestring:=s;
  259. end;
  260. procedure TM68kAssembler.WriteInstruction(hp: tai);
  261. var
  262. op : tasmop;
  263. s : string;
  264. sep : char;
  265. calljmp : boolean;
  266. i : integer;
  267. begin
  268. if hp.typ <> ait_instruction then exit;
  269. op:=taicpu(hp).opcode;
  270. calljmp:=is_calljmp(op);
  271. { call maybe not translated to call }
  272. s:=#9+getopcodestring(hp);
  273. { process operands }
  274. if taicpu(hp).ops<>0 then
  275. begin
  276. { call and jmp need an extra handling }
  277. { this code is only called if jmp isn't a labeled instruction }
  278. { quick hack to overcome a problem with manglednames=255 chars }
  279. if calljmp then
  280. begin
  281. AsmWrite(s+#9);
  282. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  283. end
  284. else
  285. begin
  286. for i:=0 to taicpu(hp).ops-1 do
  287. begin
  288. if i=0 then
  289. sep:=#9
  290. else
  291. if ((op = A_DIVSL) or
  292. (op = A_DIVUL) or
  293. (op = A_MULU) or
  294. (op = A_MULS) or
  295. (op = A_DIVS) or
  296. (op = A_DIVU)) and (i=1) then
  297. sep:=':'
  298. else
  299. sep:=',';
  300. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  301. end;
  302. end;
  303. end;
  304. AsmWriteLn(s);
  305. end;
  306. {*****************************************************************************
  307. Initialize
  308. *****************************************************************************}
  309. const
  310. as_m68k_as_info : tasminfo =
  311. (
  312. id : as_gas;
  313. idtxt : 'AS';
  314. asmbin : 'as';
  315. asmcmd : '-o $OBJ $ASM';
  316. supported_target : system_any;
  317. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  318. labelprefix : '.L';
  319. comment : '# ';
  320. );
  321. initialization
  322. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  323. end.