ra68k.pas 13 KB

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