ra68k.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. {
  2. Copyright (c) 1998-2003 by Carl Eric Codere and Peter Vreman
  3. Handles the common 68k assembler reader routines
  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 ra68k;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,aasmtai,aasmdata,aasmcpu,
  22. cpubase,rautils,cclasses;
  23. type
  24. Tm68kOperand=class(TOperand)
  25. end;
  26. Tm68kInstruction=class(TInstruction)
  27. opsize : topsize;
  28. // function ConcatInstruction(p : TAsmList):tai;override;
  29. function ConcatLabeledInstr(p : TAsmList):tai;
  30. end;
  31. implementation
  32. uses
  33. verbose,cgbase;
  34. {*****************************************************************************
  35. TM68kInstruction
  36. *****************************************************************************}
  37. {
  38. function TM68kInstruction.ConcatInstruction(p : TAsmList):tai;
  39. var
  40. fits : boolean;
  41. begin
  42. writeln('jaj mami');
  43. result:=nil;
  44. fits := FALSE;
  45. { setup specific opcodetions for first pass }
  46. { Setup special operands }
  47. { Convert to general form as to conform to the m68k opcode table }
  48. if (opcode = A_ADDA) or (opcode = A_ADDI)
  49. then opcode := A_ADD
  50. else
  51. { CMPM excluded because of GAS v1.34 BUG }
  52. if (opcode = A_CMPA) or
  53. (opcode = A_CMPI) then
  54. opcode := A_CMP
  55. else
  56. if opcode = A_EORI then
  57. opcode := A_EOR
  58. else
  59. if opcode = A_MOVEA then
  60. opcode := A_MOVE
  61. else
  62. if opcode = A_ORI then
  63. opcode := A_OR
  64. else
  65. if (opcode = A_SUBA) or (opcode = A_SUBI) then
  66. opcode := A_SUB;
  67. { Setup operand types }
  68. (*
  69. in opcode <> A_MOVEM then
  70. begin
  71. while not(fits) do
  72. begin
  73. { set the opcodetion cache, if the opcodetion }
  74. { occurs the first time }
  75. if (it[i].i=opcode) and (ins_cache[opcode]=-1) then
  76. ins_cache[opcode]:=i;
  77. if (it[i].i=opcode) and (instr.ops=it[i].ops) then
  78. begin
  79. { first fit }
  80. case instr.ops of
  81. 0 : begin
  82. fits:=true;
  83. break;
  84. end;
  85. 1 :
  86. begin
  87. if (optyp1 and it[i].o1)<>0 then
  88. begin
  89. fits:=true;
  90. break;
  91. end;
  92. end;
  93. 2 : if ((optyp1 and it[i].o1)<>0) and
  94. ((optyp2 and it[i].o2)<>0) then
  95. begin
  96. fits:=true;
  97. break;
  98. end
  99. 3 : if ((optyp1 and it[i].o1)<>0) and
  100. ((optyp2 and it[i].o2)<>0) and
  101. ((optyp3 and it[i].o3)<>0) then
  102. begin
  103. fits:=true;
  104. break;
  105. end;
  106. end; { end case }
  107. end; { endif }
  108. if it[i].i=A_NONE then
  109. begin
  110. { NO MATCH! }
  111. Message(asmr_e_invalid_combination_opcode_and_operand);
  112. exit;
  113. end;
  114. inc(i);
  115. end; { end while }
  116. *)
  117. fits:=TRUE;
  118. { We add the opcode to the opcode linked list }
  119. if fits then
  120. begin
  121. case ops of
  122. 0:
  123. if opsize <> S_NO then
  124. result:=(taicpu.op_none(opcode,opsize))
  125. else
  126. result:=(taicpu.op_none(opcode,S_NO));
  127. 1: begin
  128. case operands[1].opr.typ of
  129. OPR_SYMBOL:
  130. begin
  131. result:=(taicpu.op_sym_ofs(opcode,
  132. opsize, operands[1].opr.symbol,operands[1].opr.symofs));
  133. end;
  134. OPR_CONSTANT:
  135. begin
  136. result:=(taicpu.op_const(opcode,
  137. opsize, operands[1].opr.val));
  138. end;
  139. OPR_REGISTER:
  140. result:=(taicpu.op_reg(opcode,opsize,operands[1].opr.reg));
  141. OPR_REFERENCE:
  142. if opsize <> S_NO then
  143. begin
  144. result:=(taicpu.op_ref(opcode,
  145. opsize,operands[1].opr.ref));
  146. end
  147. else
  148. begin
  149. { special jmp and call case with }
  150. { symbolic references. }
  151. if opcode in [A_BSR,A_JMP,A_JSR,A_BRA,A_PEA] then
  152. begin
  153. result:=(taicpu.op_ref(opcode,
  154. S_NO,operands[1].opr.ref));
  155. end
  156. else
  157. Message(asmr_e_invalid_opcode_and_operand);
  158. end;
  159. OPR_NONE:
  160. Message(asmr_e_invalid_opcode_and_operand);
  161. else
  162. begin
  163. Message(asmr_e_invalid_opcode_and_operand);
  164. end;
  165. end;
  166. end;
  167. 2: begin
  168. { source }
  169. case operands[1].opr.typ of
  170. { reg,reg }
  171. { reg,ref }
  172. OPR_REGISTER:
  173. begin
  174. case operands[2].opr.typ of
  175. OPR_REGISTER:
  176. begin
  177. result:=(taicpu.op_reg_reg(opcode,
  178. opsize,operands[1].opr.reg,operands[2].opr.reg));
  179. end;
  180. OPR_REFERENCE:
  181. result:=(taicpu.op_reg_ref(opcode,
  182. opsize,operands[1].opr.reg,operands[2].opr.ref));
  183. else { else case }
  184. begin
  185. Message(asmr_e_invalid_opcode_and_operand);
  186. end;
  187. end; { end second operand case for OPR_REGISTER }
  188. end;
  189. { regset, ref }
  190. OPR_regset:
  191. begin
  192. case operands[2].opr.typ of
  193. OPR_REFERENCE :
  194. result:=(taicpu.op_regset_ref(opcode,
  195. opsize,operands[1].opr.regset,operands[2].opr.ref));
  196. else
  197. begin
  198. Message(asmr_e_invalid_opcode_and_operand);
  199. end;
  200. end; { end second operand case for OPR_regset }
  201. end;
  202. { const,reg }
  203. { const,const }
  204. { const,ref }
  205. OPR_CONSTANT:
  206. case operands[2].opr.typ of
  207. { constant, constant does not have a specific size. }
  208. OPR_CONSTANT:
  209. result:=(taicpu.op_const_const(opcode,
  210. S_NO,operands[1].opr.val,operands[2].opr.val));
  211. OPR_REFERENCE:
  212. begin
  213. result:=(taicpu.op_const_ref(opcode,
  214. opsize,operands[1].opr.val,
  215. operands[2].opr.ref))
  216. end;
  217. OPR_REGISTER:
  218. begin
  219. result:=(taicpu.op_const_reg(opcode,
  220. opsize,operands[1].opr.val,
  221. operands[2].opr.reg))
  222. end;
  223. else
  224. begin
  225. Message(asmr_e_invalid_opcode_and_operand);
  226. end;
  227. end; { end second operand case for OPR_CONSTANT }
  228. { ref,reg }
  229. { ref,ref }
  230. OPR_REFERENCE:
  231. case operands[2].opr.typ of
  232. OPR_REGISTER:
  233. begin
  234. result:=(taicpu.op_ref_reg(opcode,
  235. opsize,operands[1].opr.ref,
  236. operands[2].opr.reg));
  237. end;
  238. OPR_regset:
  239. begin
  240. result:=(taicpu.op_ref_regset(opcode,
  241. opsize,operands[1].opr.ref,
  242. operands[2].opr.regset));
  243. end;
  244. OPR_REFERENCE: { special opcodes }
  245. result:=(taicpu.op_ref_ref(opcode,
  246. opsize,operands[1].opr.ref,
  247. operands[2].opr.ref));
  248. else
  249. begin
  250. Message(asmr_e_invalid_opcode_and_operand);
  251. end;
  252. end; { end second operand case for OPR_REFERENCE }
  253. OPR_SYMBOL: case operands[2].opr.typ of
  254. OPR_REFERENCE:
  255. begin
  256. result:=(taicpu.op_sym_ofs_ref(opcode,
  257. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  258. operands[2].opr.ref))
  259. end;
  260. OPR_REGISTER:
  261. begin
  262. result:=(taicpu.op_sym_ofs_reg(opcode,
  263. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  264. operands[2].opr.reg))
  265. end;
  266. else
  267. begin
  268. Message(asmr_e_invalid_opcode_and_operand);
  269. end;
  270. end; { end second operand case for OPR_SYMBOL }
  271. else
  272. begin
  273. Message(asmr_e_invalid_opcode_and_operand);
  274. end;
  275. end; { end first operand case }
  276. end;
  277. 3: begin
  278. if (opcode = A_DIVSL) or (opcode = A_DIVUL) or (opcode = A_MULU)
  279. or (opcode = A_MULS) or (opcode = A_DIVS) or (opcode = A_DIVU) then
  280. begin
  281. if (operands[1].opr.typ <> OPR_REGISTER)
  282. or (operands[2].opr.typ <> OPR_REGISTER)
  283. or (operands[3].opr.typ <> OPR_REGISTER) then
  284. begin
  285. Message(asmr_e_invalid_opcode_and_operand);
  286. end
  287. else
  288. begin
  289. result:=(taicpu. op_reg_reg_reg(opcode,opsize,
  290. operands[1].opr.reg,operands[2].opr.reg,operands[3].opr.reg));
  291. end;
  292. end
  293. else
  294. Message(asmr_e_invalid_opcode_and_operand);
  295. end;
  296. end; { end case }
  297. end;
  298. if assigned(result) then
  299. p.concat(result);
  300. end;
  301. }
  302. function TM68kInstruction.ConcatLabeledInstr(p : TAsmList):tai;
  303. begin
  304. if ((opcode >= A_BCC) and (opcode <= A_BVS)) or
  305. (opcode = A_BRA) or (opcode = A_BSR) or
  306. (opcode = A_JMP) or (opcode = A_JSR) or
  307. ((opcode >= A_FBEQ) and (opcode <= A_FBNGLE)) then
  308. begin
  309. if ops > 2 then
  310. Message(asmr_e_invalid_opcode_and_operand)
  311. else if operands[1].opr.typ <> OPR_SYMBOL then
  312. Message(asmr_e_invalid_opcode_and_operand)
  313. else if (operands[1].opr.typ = OPR_SYMBOL) and
  314. (ops = 1) then
  315. if assigned(operands[1].opr.symbol) and
  316. (operands[1].opr.symofs=0) then
  317. result:=taicpu.op_sym(opcode,S_NO,
  318. operands[1].opr.symbol)
  319. else
  320. Message(asmr_e_invalid_opcode_and_operand);
  321. end
  322. else if ((opcode >= A_DBCC) and (opcode <= A_DBF))
  323. or ((opcode >= A_FDBEQ) and (opcode <= A_FDBNGLE)) then
  324. begin
  325. if (ops<>2) or
  326. (operands[1].opr.typ <> OPR_REGISTER) or
  327. (operands[2].opr.typ <> OPR_SYMBOL) or
  328. (operands[2].opr.symofs <> 0) then
  329. Message(asmr_e_invalid_opcode_and_operand)
  330. else
  331. result:=taicpu.op_reg_sym(opcode,opsize,operands[1].opr.reg,
  332. operands[2].opr.symbol);
  333. end
  334. else
  335. Message(asmr_e_invalid_opcode_and_operand);
  336. if assigned(result) then
  337. p.concat(result);
  338. end;
  339. end.