2
0

aasmcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. {
  2. Copyright (c) 1999-2008 by Mazen Neifer and Florian Klaempfl
  3. Contains the assembler object for the MOS Technology 6502
  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 aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,
  25. ogbase;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. instabentries = {$i mos6502nop.inc}
  32. type
  33. { Addressing modes }
  34. taddressingmode=(
  35. AM_IMPLICIT, { }
  36. AM_ACCUMULATOR, { A }
  37. AM_IMMEDIATE, { #$12 }
  38. AM_ZERO_PAGE, { $12 }
  39. AM_ZERO_PAGE_X, { $12,X }
  40. AM_ZERO_PAGE_Y, { $12,Y }
  41. AM_RELATIVE, { label }
  42. AM_ABSOLUTE, { $1234 }
  43. AM_ABSOLUTE_X, { $1234,X }
  44. AM_ABSOLUTE_Y, { $1234,Y }
  45. AM_INDIRECT, { ($1234) }
  46. AM_INDEXED_INDIRECT, { ($12,X) }
  47. AM_INDIRECT_INDEXED); { ($12),Y }
  48. tinsentry = record
  49. opcode : tasmop;
  50. adrmode : taddressingmode;
  51. code : byte;
  52. flags : longint;
  53. end;
  54. pinsentry=^tinsentry;
  55. { taicpu }
  56. taicpu = class(tai_cpu_abstract_sym)
  57. private
  58. { next fields are filled in pass1, so pass2 is faster }
  59. insentry : PInsEntry;
  60. inssize : shortint;
  61. insoffset : longint;
  62. LastInsOffset : longint;
  63. procedure init; { this need to be called by all constructors }
  64. public
  65. constructor op_none(op : tasmop);
  66. constructor op_reg(op : tasmop;_op1 : tregister);
  67. constructor op_const(op : tasmop;_op1 : LongInt);
  68. constructor op_ref(op : tasmop;const _op1 : treference);
  69. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  70. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  71. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  72. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  73. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  74. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  75. { this is for Jmp instructions }
  76. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  77. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  78. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  79. procedure loadbool(opidx:longint;_b:boolean);
  80. { register allocation }
  81. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  82. { register spilling code }
  83. function spilling_get_operation_type(opnr: longint): topertype;override;
  84. end;
  85. tai_align = class(tai_align_abstract)
  86. { nothing to add }
  87. end;
  88. procedure InitAsm;
  89. procedure DoneAsm;
  90. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  91. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  92. //function is_ref_addr16(const ref:treference): Boolean;
  93. //function is_ref_bc(const ref:treference): Boolean;
  94. //function is_ref_de(const ref:treference): Boolean;
  95. //function is_ref_hl(const ref:treference): Boolean;
  96. //function is_ref_sp(const ref:treference): Boolean;
  97. //function is_ref_ix(const ref:treference): Boolean;
  98. //function is_ref_iy(const ref:treference): Boolean;
  99. //function is_ref_ix_d(const ref:treference): Boolean;
  100. //function is_ref_iy_d(const ref:treference): Boolean;
  101. //function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  102. //function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  103. implementation
  104. {****************************************************************************
  105. Instruction table
  106. *****************************************************************************}
  107. type
  108. TInsTabCache=array[TasmOp] of longint;
  109. PInsTabCache=^TInsTabCache;
  110. const
  111. InsTab:array[0..instabentries-1] of TInsEntry={$i mos6502tab.inc}
  112. var
  113. InsTabCache : PInsTabCache;
  114. {*****************************************************************************
  115. taicpu Constructors
  116. *****************************************************************************}
  117. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  118. begin
  119. if opidx>=ops then
  120. ops:=opidx+1;
  121. with oper[opidx]^ do
  122. begin
  123. if typ=top_ref then
  124. dispose(ref);
  125. b:=_b;
  126. typ:=top_bool;
  127. end;
  128. end;
  129. procedure taicpu.init;
  130. begin
  131. insentry:=nil;
  132. LastInsOffset:=-1;
  133. InsOffset:=0;
  134. InsSize:=0;
  135. end;
  136. constructor taicpu.op_none(op : tasmop);
  137. begin
  138. inherited create(op);
  139. init;
  140. end;
  141. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  142. begin
  143. inherited create(op);
  144. init;
  145. ops:=1;
  146. loadreg(0,_op1);
  147. end;
  148. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  149. begin
  150. inherited create(op);
  151. init;
  152. ops:=1;
  153. loadref(0,_op1);
  154. end;
  155. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  156. begin
  157. inherited create(op);
  158. init;
  159. ops:=1;
  160. loadconst(0,_op1);
  161. end;
  162. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  163. begin
  164. inherited create(op);
  165. init;
  166. ops:=2;
  167. loadreg(0,_op1);
  168. loadreg(1,_op2);
  169. end;
  170. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  171. begin
  172. inherited create(op);
  173. init;
  174. ops:=2;
  175. loadreg(0,_op1);
  176. loadconst(1,_op2);
  177. end;
  178. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  179. begin
  180. inherited create(op);
  181. init;
  182. ops:=2;
  183. loadconst(0,_op1);
  184. loadreg(1,_op2);
  185. end;
  186. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  187. begin
  188. inherited create(op);
  189. init;
  190. ops:=2;
  191. loadreg(0,_op1);
  192. loadref(1,_op2);
  193. end;
  194. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  195. begin
  196. inherited create(op);
  197. init;
  198. ops:=2;
  199. loadref(0,_op1);
  200. loadreg(1,_op2);
  201. end;
  202. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  203. begin
  204. inherited create(op);
  205. init;
  206. ops:=2;
  207. loadref(0,_op1);
  208. loadconst(1,_op2);
  209. end;
  210. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  211. begin
  212. inherited create(op);
  213. init;
  214. is_jmp:=op in jmp_instructions;
  215. condition:=cond;
  216. ops:=1;
  217. loadsymbol(0,_op1,0);
  218. end;
  219. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  220. begin
  221. inherited create(op);
  222. init;
  223. is_jmp:=op in jmp_instructions;
  224. ops:=1;
  225. loadsymbol(0,_op1,0);
  226. end;
  227. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  228. begin
  229. inherited create(op);
  230. init;
  231. ops:=1;
  232. loadsymbol(0,_op1,_op1ofs);
  233. end;
  234. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  235. begin
  236. result:=false;
  237. end;
  238. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  239. begin
  240. internalerror(2024040602);
  241. end;
  242. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  243. begin
  244. internalerror(2024040603);
  245. //case getregtype(r) of
  246. // R_INTREGISTER :
  247. // result:=taicpu.op_reg_ref(A_LD,r,ref)
  248. // else
  249. // internalerror(200401041);
  250. //end;
  251. end;
  252. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  253. begin
  254. internalerror(2024040604);
  255. //case getregtype(r) of
  256. // R_INTREGISTER :
  257. // result:=taicpu.op_ref_reg(A_LD,ref,r);
  258. // else
  259. // internalerror(2004010403);
  260. //end;
  261. end;
  262. //function is_ref_addr16(const ref: treference): Boolean;
  263. // begin
  264. // result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  265. // end;
  266. //
  267. //
  268. //function is_ref_bc(const ref: treference): Boolean;
  269. // begin
  270. // result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  271. // ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  272. // (ref.offset=0) and (ref.scalefactor<=1) and
  273. // (ref.symbol=nil) and (ref.relsymbol=nil);
  274. // end;
  275. //
  276. //
  277. //function is_ref_de(const ref: treference): Boolean;
  278. // begin
  279. // result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  280. // ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  281. // (ref.offset=0) and (ref.scalefactor<=1) and
  282. // (ref.symbol=nil) and (ref.relsymbol=nil);
  283. // end;
  284. //
  285. //
  286. //function is_ref_hl(const ref: treference): Boolean;
  287. // begin
  288. // result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  289. // ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  290. // (ref.offset=0) and (ref.scalefactor<=1) and
  291. // (ref.symbol=nil) and (ref.relsymbol=nil);
  292. // end;
  293. //
  294. //
  295. //function is_ref_sp(const ref: treference): Boolean;
  296. // begin
  297. // result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  298. // ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  299. // (ref.offset=0) and (ref.scalefactor<=1) and
  300. // (ref.symbol=nil) and (ref.relsymbol=nil);
  301. // end;
  302. //
  303. //
  304. //function is_ref_ix(const ref: treference): Boolean;
  305. // begin
  306. // result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  307. // ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  308. // (ref.offset=0) and (ref.scalefactor<=1) and
  309. // (ref.symbol=nil) and (ref.relsymbol=nil);
  310. // end;
  311. //
  312. //
  313. //function is_ref_iy(const ref: treference): Boolean;
  314. // begin
  315. // result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  316. // ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  317. // (ref.offset=0) and (ref.scalefactor<=1) and
  318. // (ref.symbol=nil) and (ref.relsymbol=nil);
  319. // end;
  320. //
  321. //
  322. //function is_ref_ix_d(const ref: treference): Boolean;
  323. // begin
  324. // result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  325. // ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  326. // (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  327. // (ref.symbol=nil) and (ref.relsymbol=nil);
  328. // end;
  329. //
  330. //
  331. //function is_ref_iy_d(const ref: treference): Boolean;
  332. // begin
  333. // result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  334. // ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  335. // (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  336. // (ref.symbol=nil) and (ref.relsymbol=nil);
  337. // end;
  338. //
  339. //
  340. //function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  341. // begin
  342. // case opertype of
  343. // OT_REF_ADDR16:
  344. // result:=is_ref_addr16(ref);
  345. // OT_REF_BC:
  346. // result:=is_ref_bc(ref);
  347. // OT_REF_DE:
  348. // result:=is_ref_de(ref);
  349. // OT_REF_HL:
  350. // result:=is_ref_hl(ref);
  351. // OT_REF_SP:
  352. // result:=is_ref_sp(ref);
  353. // OT_REF_IX:
  354. // result:=is_ref_ix(ref);
  355. // OT_REF_IY:
  356. // result:=is_ref_iy(ref);
  357. // OT_REF_IX_d:
  358. // result:=is_ref_ix_d(ref);
  359. // OT_REF_IY_d:
  360. // result:=is_ref_iy_d(ref);
  361. // else
  362. // internalerror(2020041801);
  363. // end;
  364. // end;
  365. //
  366. //
  367. //function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  368. // var
  369. // ot: trefoperandtype;
  370. // begin
  371. // result:=true;
  372. // for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  373. // if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  374. // exit;
  375. // result:=false;
  376. // end;
  377. {****************************************************************************
  378. Instruction table
  379. *****************************************************************************}
  380. procedure BuildInsTabCache;
  381. var
  382. i : longint;
  383. begin
  384. new(instabcache);
  385. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  386. i:=0;
  387. while (i<InsTabEntries) do
  388. begin
  389. if InsTabCache^[InsTab[i].OPcode]=-1 then
  390. InsTabCache^[InsTab[i].OPcode]:=i;
  391. inc(i);
  392. end;
  393. end;
  394. procedure InitAsm;
  395. begin
  396. if not assigned(instabcache) then
  397. BuildInsTabCache;
  398. end;
  399. procedure DoneAsm;
  400. begin
  401. if assigned(instabcache) then
  402. begin
  403. dispose(instabcache);
  404. instabcache:=nil;
  405. end;
  406. end;
  407. begin
  408. cai_cpu:=taicpu;
  409. cai_align:=tai_align;
  410. end.