agcpugas.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. gas_reg2str : reg2strtable =
  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. implementation
  95. uses
  96. cutils,systems,
  97. verbose;
  98. function getreferencestring(var ref : treference) : string;
  99. var
  100. s,basestr,indexstr : string;
  101. begin
  102. s:='';
  103. with ref do
  104. begin
  105. inc(offset,offsetfixup);
  106. offsetfixup:=0;
  107. basestr:=gas_reg2str[base.enum];
  108. indexstr:=gas_reg2str[index.enum];
  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.enum=R_NO) and (base.enum=R_NO) and not assigned(symbol) then
  118. s:=s+'0';
  119. if (index.enum<>R_NO) and (base.enum=R_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.enum=R_NO) and (base.enum<>R_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.enum=R_NO) and (base.enum<>R_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.enum=R_NO) and (base.enum<>R_NO) and (direction=dir_none) then
  141. begin
  142. s:=s+'('+basestr+')'
  143. end
  144. else if (index.enum<>R_NO) and (base.enum<>R_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 : getopstr:=gas_reg2str[o.reg.enum];
  161. top_ref : getopstr:=getreferencestring(o.ref^);
  162. top_reglist : begin
  163. hs:='';
  164. for i:=first_supreg to last_supreg do
  165. begin
  166. if i in o.registerlist then
  167. hs:=hs+supreg_name(i)+'/';
  168. end;
  169. delete(hs,length(hs),1);
  170. getopstr := hs;
  171. end;
  172. top_const : getopstr:='#'+tostr(longint(o.val));
  173. top_symbol :
  174. { compare with i386, where a symbol is considered }
  175. { a constant. }
  176. begin
  177. if assigned(o.sym) then
  178. hs:='#'+o.sym.name
  179. else
  180. hs:='#';
  181. if o.symofs>0 then
  182. hs:=hs+'+'+tostr(o.symofs)
  183. else
  184. if o.symofs<0 then
  185. hs:=hs+tostr(o.symofs)
  186. else
  187. if not(assigned(o.sym)) then
  188. hs:=hs+'0';
  189. getopstr:=hs;
  190. end;
  191. else internalerror(10001);
  192. end;
  193. end;
  194. function getopstr_jmp(const o:toper) : string;
  195. var
  196. hs : string;
  197. begin
  198. case o.typ of
  199. top_reg : getopstr_jmp:=gas_reg2str[o.reg.enum];
  200. top_ref : getopstr_jmp:=getreferencestring(o.ref^);
  201. top_const : getopstr_jmp:=tostr(o.val);
  202. top_symbol : begin
  203. if assigned(o.sym) then
  204. hs:=o.sym.name
  205. else
  206. hs:='';
  207. if o.symofs>0 then
  208. hs:=hs+'+'+tostr(o.symofs)
  209. else
  210. if o.symofs<0 then
  211. hs:=hs+tostr(o.symofs)
  212. else
  213. if not(assigned(o.sym)) then
  214. hs:=hs+'0';
  215. getopstr_jmp:=hs;
  216. end;
  217. else internalerror(10001);
  218. end;
  219. end;
  220. {****************************************************************************
  221. TM68kASMOUTPUT
  222. ****************************************************************************}
  223. (*
  224. ait_instruction : begin
  225. { old versions of GAS don't like PEA.L and LEA.L }
  226. if (paicpu(hp)^.opcode in [
  227. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  228. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  229. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  230. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  231. s:=#9+mot_op2str[paicpu(hp)^.opcode]
  232. else
  233. s:=#9+mot_op2str[paicpu(hp)^.opcode]+mit_opsize2str[paicpu(hp)^.opsize];
  234. if paicpu(hp)^.ops>0 then
  235. begin
  236. { call and jmp need an extra handling }
  237. { this code is only callded if jmp isn't a labeled instruction }
  238. if paicpu(hp)^.opcode in [A_BSR,A_BRA,A_LEA,A_PEA,A_JSR,A_JMP] then
  239. s:=s+#9#9+getopstr_jmp(paicpu(hp)^.oper[0])
  240. else
  241. s:=s+#9+getopstr(paicpu(hp)^.oper[0]);
  242. if paicpu(hp)^.ops>1 then
  243. begin
  244. s:=s+','+getopstr(paicpu(hp)^.oper[1]);
  245. { three operands }
  246. if paicpu(hp)^.ops>2 then
  247. begin
  248. if (paicpu(hp)^.opcode = A_DIVSL) or
  249. (paicpu(hp)^.opcode = A_DIVUL) or
  250. (paicpu(hp)^.opcode = A_MULU) or
  251. (paicpu(hp)^.opcode = A_MULS) or
  252. (paicpu(hp)^.opcode = A_DIVS) or
  253. (paicpu(hp)^.opcode = A_DIVU) then
  254. s:=s+':'+getopstr(paicpu(hp)^.oper[2])
  255. else
  256. s:=s+','+getopstr(paicpu(hp)^.oper[2]);
  257. end;
  258. end;
  259. end;
  260. AsmWriteLn(s);
  261. end;
  262. ait_labeled_instruction : begin
  263. { labeled operand }
  264. if pai_labeled(hp)^.register = R_NO then
  265. begin
  266. if pai_labeled(hp)^.lab <> nil then
  267. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+pai_labeled(hp)^.lab^.name)
  268. else
  269. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+pai_labeled(hp)^.sym^.name);
  270. end
  271. else
  272. { labeled operand with register }
  273. begin
  274. if pai_labeled(hp)^.lab <> nil then
  275. begin
  276. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+
  277. gas_reg2str[pai_labeled(hp)^.register]+','+pai_labeled(hp)^.lab^.name);
  278. end
  279. else
  280. { a symbol is the value }
  281. begin
  282. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+
  283. gas_reg2str[pai_labeled(hp)^.register]+','+pai_labeled(hp)^.sym^.name);
  284. end;
  285. end;
  286. end;
  287. *)
  288. { returns the opcode string }
  289. function getopcodestring(hp : tai) : string;
  290. var
  291. op : tasmop;
  292. s : string;
  293. begin
  294. op:=taicpu(hp).opcode;
  295. { old versions of GAS don't like PEA.L and LEA.L }
  296. if (op in [
  297. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  298. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  299. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  300. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  301. s:=gas_op2str[op]
  302. else
  303. if op = A_SXX then
  304. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  305. else
  306. if op in [a_dbxx,a_bxx,a_fbxx] then
  307. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  308. else
  309. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  310. getopcodestring:=s;
  311. end;
  312. procedure TM68kAssembler. WriteInstruction(hp: tai);
  313. var
  314. op : tasmop;
  315. s : string;
  316. sep : char;
  317. calljmp : boolean;
  318. i : integer;
  319. begin
  320. if hp.typ <> ait_instruction then exit;
  321. op:=taicpu(hp).opcode;
  322. calljmp:=is_calljmp(op);
  323. { call maybe not translated to call }
  324. s:=#9+getopcodestring(hp);
  325. { process operands }
  326. if taicpu(hp).ops<>0 then
  327. begin
  328. { call and jmp need an extra handling }
  329. { this code is only called if jmp isn't a labeled instruction }
  330. { quick hack to overcome a problem with manglednames=255 chars }
  331. if calljmp then
  332. begin
  333. AsmWrite(s+#9);
  334. s:=getopstr_jmp(taicpu(hp).oper[0]);
  335. end
  336. else
  337. begin
  338. for i:=0 to taicpu(hp).ops-1 do
  339. begin
  340. if i=0 then
  341. sep:=#9
  342. else
  343. if ((op = A_DIVSL) or
  344. (op = A_DIVUL) or
  345. (op = A_MULU) or
  346. (op = A_MULS) or
  347. (op = A_DIVS) or
  348. (op = A_DIVU)) and (i=1) then
  349. sep:=':'
  350. else
  351. sep:=',';
  352. s:=s+sep+getopstr(taicpu(hp).oper[i])
  353. end;
  354. end;
  355. end;
  356. AsmWriteLn(s);
  357. end;
  358. {*****************************************************************************
  359. Initialize
  360. *****************************************************************************}
  361. const
  362. as_m68k_as_info : tasminfo =
  363. (
  364. id : as_gas;
  365. idtxt : 'AS';
  366. asmbin : 'as';
  367. asmcmd : '-o $OBJ $ASM';
  368. supported_target : system_any;
  369. outputbinary: false;
  370. allowdirect : true;
  371. needar : true;
  372. labelprefix_only_inside_procedure : false;
  373. labelprefix : '.L';
  374. comment : '# ';
  375. secnames : ('',
  376. '.text','.data','.bss',
  377. '','','','','','',
  378. '.stab','.stabstr','COMMON')
  379. );
  380. initialization
  381. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  382. end.
  383. {
  384. $Log$
  385. Revision 1.7 2003-02-19 22:00:16 daniel
  386. * Code generator converted to new register notation
  387. - Horribily outdated todo.txt removed
  388. Revision 1.6 2003/02/15 22:19:40 carl
  389. * bugfix of emissions of jmp instructions
  390. Revision 1.5 2003/01/08 18:43:57 daniel
  391. * Tregister changed into a record
  392. Revision 1.4 2002/11/30 23:33:02 carl
  393. * merges from Pierre's fixes in m68k fixes branch
  394. Revision 1.3 2002/09/07 15:25:11 peter
  395. * old logs removed and tabs fixed
  396. Revision 1.2 2002/08/13 18:58:54 carl
  397. + m68k problems with cvs fixed?()!
  398. }