agcpugas.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. { 68000 only instructions }
  35. ('abcd','add', 'adda','addi','addq','addx','and','andi',
  36. 'asl','asr','bcc','bcs','beq','bge','bgt','bhi',
  37. 'ble','bls','blt','bmi','bne','bpl','bvc','bvs',
  38. 'bchg','bclr','bra','bset','bsr','btst','chk',
  39. 'clr','cmp','cmpa','cmpi','cmpm','dbcc','dbcs','dbeq','dbge',
  40. 'dbgt','dbhi','dble','dbls','dblt','dbmi','dbne','dbra',
  41. 'dbpl','dbt','dbvc','dbvs','dbf','divs','divu',
  42. 'eor','eori','exg','illegal','ext','jmp','jsr',
  43. 'lea','link','lsl','lsr','move','movea','movei','moveq',
  44. 'movem','movep','muls','mulu','nbcd','neg','negx',
  45. 'nop','not','or','ori','pea','rol','ror','roxl',
  46. 'roxr','rtr','rts','sbcd','scc','scs','seq','sge',
  47. 'sgt','shi','sle','sls','slt','smi','sne',
  48. 'spl','st','svc','svs','sf','sub','suba','subi','subq',
  49. 'subx','swap','tas','trap','trapv','tst','unlk',
  50. 'rte','reset','stop',
  51. { MC68010 instructions }
  52. 'bkpt','movec','moves','rtd',
  53. { MC68020 instructions }
  54. 'bfchg','bfclr','bfexts','bfextu','bfffo',
  55. 'bfins','bfset','bftst','callm','cas','cas2',
  56. 'chk2','cmp2','divsl','divul','extb','pack','rtm',
  57. 'trapcc','tracs','trapeq','trapf','trapge','trapgt',
  58. 'traphi','traple','trapls','traplt','trapmi','trapne',
  59. 'trappl','trapt','trapvc','trapvs','unpk',
  60. { FPU Processor instructions - directly supported only. }
  61. { IEEE aware and misc. condition codes not supported }
  62. 'fabs','fadd',
  63. 'fbeq','fbne','fbngt','fbgt','fbge','fbnge',
  64. 'fblt','fbnlt','fble','fbgl','fbngl','fbgle','fbngle',
  65. 'fdbeq','fdbne','fdbgt','fdbngt','fdbge','fdnbge',
  66. 'fdblt','fdbnlt','fdble','fdbgl','fdbngl','fdbgle','fbdngle',
  67. 'fseq','fsne','fsgt','fsngt','fsge','fsnge',
  68. 'fslt','fsnlt','fsle','fsgl','fsngl','fsgle','fsngle',
  69. 'fcmp','fdiv','fmove','fmovem',
  70. 'fmul','fneg','fnop','fsqrt','fsub','fsgldiv',
  71. 'fsflmul','ftst',
  72. 'fint','fintrz',
  73. 'ftrapeq','ftrapne','ftrapgt','ftrapngt','ftrapge','ftrapnge',
  74. 'ftraplt','ftrapnlt','ftraple','ftrapgl','ftrapngl','ftrapgle',
  75. 'ftrapngle',
  76. { Useful for assembly langage output }
  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 langage output }
  84. '','','','');
  85. gas_opsize2str : array[topsize] of string[2] =
  86. ('','.b','.w','.l','.s','.d','.x',''
  87. );
  88. gas_reg2str : reg2strtable =
  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. implementation
  96. uses
  97. cutils,systems,
  98. verbose;
  99. function getreferencestring(var ref : treference) : string;
  100. var
  101. s,basestr,indexstr : string;
  102. begin
  103. s:='';
  104. with ref do
  105. begin
  106. inc(offset,offsetfixup);
  107. offsetfixup:=0;
  108. basestr:=gas_reg2str[base];
  109. indexstr:=gas_reg2str[index];
  110. if assigned(symbol) then
  111. s:=s+symbol.name;
  112. if offset<0 then s:=s+tostr(offset)
  113. else if (offset>0) then
  114. begin
  115. if (symbol=nil) then s:=tostr(offset)
  116. else s:=s+'+'+tostr(offset);
  117. end
  118. else if (index=R_NO) and (base=R_NO) and not assigned(symbol) then
  119. s:=s+'0';
  120. if (index<>R_NO) and (base=R_NO) and (direction=dir_none) then
  121. begin
  122. if (scalefactor = 1) or (scalefactor = 0) then
  123. s:=s+'(,'+indexstr+'.l)'
  124. else
  125. s:=s+'(,'+indexstr+'.l*'+tostr(scalefactor)+')'
  126. end
  127. else if (index=R_NO) and (base<>R_NO) and (direction=dir_inc) then
  128. begin
  129. if (scalefactor = 1) or (scalefactor = 0) then
  130. s:=s+'('+basestr+')+'
  131. else
  132. InternalError(10002);
  133. end
  134. else if (index=R_NO) and (base<>R_NO) and (direction=dir_dec) then
  135. begin
  136. if (scalefactor = 1) or (scalefactor = 0) then
  137. s:=s+'-('+basestr+')'
  138. else
  139. InternalError(10003);
  140. end
  141. else if (index=R_NO) and (base<>R_NO) and (direction=dir_none) then
  142. begin
  143. s:=s+'('+basestr+')'
  144. end
  145. else if (index<>R_NO) and (base<>R_NO) and (direction=dir_none) then
  146. begin
  147. if (scalefactor = 1) or (scalefactor = 0) then
  148. s:=s+'('+basestr+','+indexstr+'.l)'
  149. else
  150. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  151. end;
  152. end;
  153. getreferencestring:=s;
  154. end;
  155. function getopstr(const o:toper) : string;
  156. var
  157. hs : string;
  158. i : tregister;
  159. begin
  160. case o.typ of
  161. top_reg : getopstr:=gas_reg2str[o.reg];
  162. top_ref : getopstr:=getreferencestring(o.ref^);
  163. top_reglist : begin
  164. hs:='';
  165. for i:=R_NO to R_FPSR do
  166. begin
  167. if i in o.registerlist then
  168. hs:=hs+gas_reg2str[i]+'/';
  169. end;
  170. delete(hs,length(hs),1);
  171. getopstr := hs;
  172. end;
  173. top_const : getopstr:='#'+tostr(o.val);
  174. top_symbol :
  175. { compare with i386, where a symbol is considered }
  176. { a constant. }
  177. begin
  178. if assigned(o.sym) then
  179. hs:='#'+o.sym.name
  180. else
  181. hs:='#';
  182. if o.symofs>0 then
  183. hs:=hs+'+'+tostr(o.symofs)
  184. else
  185. if o.symofs<0 then
  186. hs:=hs+tostr(o.symofs)
  187. else
  188. if not(assigned(o.sym)) then
  189. hs:=hs+'0';
  190. getopstr:=hs;
  191. end;
  192. else internalerror(10001);
  193. end;
  194. end;
  195. function getopstr_jmp(const o:toper) : string;
  196. var
  197. hs : string;
  198. begin
  199. case o.typ of
  200. top_reg : getopstr_jmp:=gas_reg2str[o.reg];
  201. top_ref : getopstr_jmp:=getreferencestring(o.ref^);
  202. top_const : getopstr_jmp:=tostr(o.val);
  203. top_symbol : begin
  204. if assigned(o.sym) then
  205. hs:=o.sym.name
  206. else
  207. hs:='';
  208. if o.symofs>0 then
  209. hs:=hs+'+'+tostr(o.symofs)
  210. else
  211. if o.symofs<0 then
  212. hs:=hs+tostr(o.symofs)
  213. else
  214. if not(assigned(o.sym)) then
  215. hs:=hs+'0';
  216. getopstr_jmp:=hs;
  217. end;
  218. else internalerror(10001);
  219. end;
  220. end;
  221. {****************************************************************************
  222. TM68kASMOUTPUT
  223. ****************************************************************************}
  224. (*
  225. ait_instruction : begin
  226. { old versions of GAS don't like PEA.L and LEA.L }
  227. if (paicpu(hp)^.opcode in [
  228. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  229. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  230. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  231. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  232. s:=#9+mot_op2str[paicpu(hp)^.opcode]
  233. else
  234. s:=#9+mot_op2str[paicpu(hp)^.opcode]+mit_opsize2str[paicpu(hp)^.opsize];
  235. if paicpu(hp)^.ops>0 then
  236. begin
  237. { call and jmp need an extra handling }
  238. { this code is only callded if jmp isn't a labeled instruction }
  239. if paicpu(hp)^.opcode in [A_BSR,A_BRA,A_LEA,A_PEA,A_JSR,A_JMP] then
  240. s:=s+#9#9+getopstr_jmp(paicpu(hp)^.oper[0])
  241. else
  242. s:=s+#9+getopstr(paicpu(hp)^.oper[0]);
  243. if paicpu(hp)^.ops>1 then
  244. begin
  245. s:=s+','+getopstr(paicpu(hp)^.oper[1]);
  246. { three operands }
  247. if paicpu(hp)^.ops>2 then
  248. begin
  249. if (paicpu(hp)^.opcode = A_DIVSL) or
  250. (paicpu(hp)^.opcode = A_DIVUL) or
  251. (paicpu(hp)^.opcode = A_MULU) or
  252. (paicpu(hp)^.opcode = A_MULS) or
  253. (paicpu(hp)^.opcode = A_DIVS) or
  254. (paicpu(hp)^.opcode = A_DIVU) then
  255. s:=s+':'+getopstr(paicpu(hp)^.oper[2])
  256. else
  257. s:=s+','+getopstr(paicpu(hp)^.oper[2]);
  258. end;
  259. end;
  260. end;
  261. AsmWriteLn(s);
  262. end;
  263. ait_labeled_instruction : begin
  264. { labeled operand }
  265. if pai_labeled(hp)^.register = R_NO then
  266. begin
  267. if pai_labeled(hp)^.lab <> nil then
  268. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+pai_labeled(hp)^.lab^.name)
  269. else
  270. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+pai_labeled(hp)^.sym^.name);
  271. end
  272. else
  273. { labeled operand with register }
  274. begin
  275. if pai_labeled(hp)^.lab <> nil then
  276. begin
  277. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+
  278. gas_reg2str[pai_labeled(hp)^.register]+','+pai_labeled(hp)^.lab^.name);
  279. end
  280. else
  281. { a symbol is the value }
  282. begin
  283. AsmWriteLn(#9+mot_op2str[pai_labeled(hp)^.opcode]+#9+
  284. gas_reg2str[pai_labeled(hp)^.register]+','+pai_labeled(hp)^.sym^.name);
  285. end;
  286. end;
  287. end;
  288. *)
  289. { returns the opcode string }
  290. function getopcodestring(hp : tai) : string;
  291. var
  292. op : tasmop;
  293. s : string;
  294. begin
  295. op:=taicpu(hp).opcode;
  296. { old versions of GAS don't like PEA.L and LEA.L }
  297. if (op in [
  298. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  299. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  300. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  301. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  302. s:=gas_op2str[op]
  303. else
  304. if op = A_SXX then
  305. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  306. else
  307. if op in [a_dbxx,a_bxx,a_fbxx] then
  308. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  309. else
  310. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  311. getopcodestring:=s;
  312. end;
  313. procedure TM68kAssembler. WriteInstruction(hp: tai);
  314. var
  315. op : tasmop;
  316. s : string;
  317. sep : char;
  318. calljmp : boolean;
  319. i : integer;
  320. begin
  321. if hp.typ <> ait_instruction then exit;
  322. op:=taicpu(hp).opcode;
  323. calljmp:=is_calljmp(op);
  324. { call maybe not translated to call }
  325. s:=#9+getopcodestring(hp);
  326. { process operands }
  327. if taicpu(hp).ops<>0 then
  328. begin
  329. { call and jmp need an extra handling }
  330. { this code is only called if jmp isn't a labeled instruction }
  331. { quick hack to overcome a problem with manglednames=255 chars }
  332. if calljmp then
  333. begin
  334. AsmWrite(s+#9);
  335. s:=getopstr_jmp(taicpu(hp).oper[0]);
  336. end
  337. else
  338. begin
  339. for i:=0 to taicpu(hp).ops-1 do
  340. begin
  341. if i=0 then
  342. sep:=#9
  343. else
  344. if ((op = A_DIVSL) or
  345. (op = A_DIVUL) or
  346. (op = A_MULU) or
  347. (op = A_MULS) or
  348. (op = A_DIVS) or
  349. (op = A_DIVU)) and (i=1) then
  350. sep:=':'
  351. else
  352. sep:=',';
  353. s:=s+sep+getopstr(taicpu(hp).oper[i])
  354. end;
  355. end;
  356. end;
  357. AsmWriteLn(s);
  358. end;
  359. {*****************************************************************************
  360. Initialize
  361. *****************************************************************************}
  362. const
  363. as_m68k_as_info : tasminfo =
  364. (
  365. id : as_gas;
  366. idtxt : 'AS';
  367. asmbin : 'as';
  368. asmcmd : '-o $OBJ $ASM';
  369. supported_target : system_any;
  370. outputbinary: false;
  371. allowdirect : true;
  372. needar : true;
  373. labelprefix_only_inside_procedure : false;
  374. labelprefix : '.L';
  375. comment : '# ';
  376. secnames : ('',
  377. '.text','.data','.bss',
  378. '','','','','','',
  379. '.stab','.stabstr','COMMON')
  380. );
  381. initialization
  382. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  383. end.
  384. {
  385. $Log$
  386. Revision 1.1 2002-08-13 18:01:52 carl
  387. * rename swatoperands to swapoperands
  388. + m68k first compilable version (still needs a lot of testing):
  389. assembler generator, system information , inline
  390. assembler reader.
  391. }