aasmcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. end;
  83. tai_align = class(tai_align_abstract)
  84. { nothing to add }
  85. end;
  86. procedure InitAsm;
  87. procedure DoneAsm;
  88. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  89. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  90. //function is_ref_addr16(const ref:treference): Boolean;
  91. //function is_ref_bc(const ref:treference): Boolean;
  92. //function is_ref_de(const ref:treference): Boolean;
  93. //function is_ref_hl(const ref:treference): Boolean;
  94. //function is_ref_sp(const ref:treference): Boolean;
  95. //function is_ref_ix(const ref:treference): Boolean;
  96. //function is_ref_iy(const ref:treference): Boolean;
  97. //function is_ref_ix_d(const ref:treference): Boolean;
  98. //function is_ref_iy_d(const ref:treference): Boolean;
  99. //function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  100. //function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  101. implementation
  102. {****************************************************************************
  103. Instruction table
  104. *****************************************************************************}
  105. type
  106. TInsTabCache=array[TasmOp] of longint;
  107. PInsTabCache=^TInsTabCache;
  108. const
  109. InsTab:array[0..instabentries-1] of TInsEntry={$i mos6502tab.inc}
  110. var
  111. InsTabCache : PInsTabCache;
  112. {*****************************************************************************
  113. taicpu Constructors
  114. *****************************************************************************}
  115. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  116. begin
  117. if opidx>=ops then
  118. ops:=opidx+1;
  119. with oper[opidx]^ do
  120. begin
  121. if typ=top_ref then
  122. dispose(ref);
  123. b:=_b;
  124. typ:=top_bool;
  125. end;
  126. end;
  127. procedure taicpu.init;
  128. begin
  129. insentry:=nil;
  130. LastInsOffset:=-1;
  131. InsOffset:=0;
  132. InsSize:=0;
  133. end;
  134. constructor taicpu.op_none(op : tasmop);
  135. begin
  136. inherited create(op);
  137. init;
  138. end;
  139. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  140. begin
  141. inherited create(op);
  142. init;
  143. ops:=1;
  144. loadreg(0,_op1);
  145. end;
  146. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  147. begin
  148. inherited create(op);
  149. init;
  150. ops:=1;
  151. loadref(0,_op1);
  152. end;
  153. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  154. begin
  155. inherited create(op);
  156. init;
  157. ops:=1;
  158. loadconst(0,_op1);
  159. end;
  160. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  161. begin
  162. inherited create(op);
  163. init;
  164. ops:=2;
  165. loadreg(0,_op1);
  166. loadreg(1,_op2);
  167. end;
  168. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  169. begin
  170. inherited create(op);
  171. init;
  172. ops:=2;
  173. loadreg(0,_op1);
  174. loadconst(1,_op2);
  175. end;
  176. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  177. begin
  178. inherited create(op);
  179. init;
  180. ops:=2;
  181. loadconst(0,_op1);
  182. loadreg(1,_op2);
  183. end;
  184. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  185. begin
  186. inherited create(op);
  187. init;
  188. ops:=2;
  189. loadreg(0,_op1);
  190. loadref(1,_op2);
  191. end;
  192. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  193. begin
  194. inherited create(op);
  195. init;
  196. ops:=2;
  197. loadref(0,_op1);
  198. loadreg(1,_op2);
  199. end;
  200. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  201. begin
  202. inherited create(op);
  203. init;
  204. ops:=2;
  205. loadref(0,_op1);
  206. loadconst(1,_op2);
  207. end;
  208. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  209. begin
  210. inherited create(op);
  211. init;
  212. is_jmp:=op in jmp_instructions;
  213. condition:=cond;
  214. ops:=1;
  215. loadsymbol(0,_op1,0);
  216. end;
  217. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  218. begin
  219. inherited create(op);
  220. init;
  221. is_jmp:=op in jmp_instructions;
  222. ops:=1;
  223. loadsymbol(0,_op1,0);
  224. end;
  225. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  226. begin
  227. inherited create(op);
  228. init;
  229. ops:=1;
  230. loadsymbol(0,_op1,_op1ofs);
  231. end;
  232. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  233. begin
  234. result:=false;
  235. end;
  236. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  237. begin
  238. internalerror(2024040603);
  239. //case getregtype(r) of
  240. // R_INTREGISTER :
  241. // result:=taicpu.op_reg_ref(A_LD,r,ref)
  242. // else
  243. // internalerror(200401041);
  244. //end;
  245. end;
  246. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  247. begin
  248. internalerror(2024040604);
  249. //case getregtype(r) of
  250. // R_INTREGISTER :
  251. // result:=taicpu.op_ref_reg(A_LD,ref,r);
  252. // else
  253. // internalerror(2004010403);
  254. //end;
  255. end;
  256. //function is_ref_addr16(const ref: treference): Boolean;
  257. // begin
  258. // result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  259. // end;
  260. //
  261. //
  262. //function is_ref_bc(const ref: treference): Boolean;
  263. // begin
  264. // result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  265. // ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  266. // (ref.offset=0) and (ref.scalefactor<=1) and
  267. // (ref.symbol=nil) and (ref.relsymbol=nil);
  268. // end;
  269. //
  270. //
  271. //function is_ref_de(const ref: treference): Boolean;
  272. // begin
  273. // result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  274. // ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  275. // (ref.offset=0) and (ref.scalefactor<=1) and
  276. // (ref.symbol=nil) and (ref.relsymbol=nil);
  277. // end;
  278. //
  279. //
  280. //function is_ref_hl(const ref: treference): Boolean;
  281. // begin
  282. // result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  283. // ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  284. // (ref.offset=0) and (ref.scalefactor<=1) and
  285. // (ref.symbol=nil) and (ref.relsymbol=nil);
  286. // end;
  287. //
  288. //
  289. //function is_ref_sp(const ref: treference): Boolean;
  290. // begin
  291. // result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  292. // ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  293. // (ref.offset=0) and (ref.scalefactor<=1) and
  294. // (ref.symbol=nil) and (ref.relsymbol=nil);
  295. // end;
  296. //
  297. //
  298. //function is_ref_ix(const ref: treference): Boolean;
  299. // begin
  300. // result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  301. // ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  302. // (ref.offset=0) and (ref.scalefactor<=1) and
  303. // (ref.symbol=nil) and (ref.relsymbol=nil);
  304. // end;
  305. //
  306. //
  307. //function is_ref_iy(const ref: treference): Boolean;
  308. // begin
  309. // result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  310. // ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  311. // (ref.offset=0) and (ref.scalefactor<=1) and
  312. // (ref.symbol=nil) and (ref.relsymbol=nil);
  313. // end;
  314. //
  315. //
  316. //function is_ref_ix_d(const ref: treference): Boolean;
  317. // begin
  318. // result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  319. // ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  320. // (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  321. // (ref.symbol=nil) and (ref.relsymbol=nil);
  322. // end;
  323. //
  324. //
  325. //function is_ref_iy_d(const ref: treference): Boolean;
  326. // begin
  327. // result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  328. // ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  329. // (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  330. // (ref.symbol=nil) and (ref.relsymbol=nil);
  331. // end;
  332. //
  333. //
  334. //function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  335. // begin
  336. // case opertype of
  337. // OT_REF_ADDR16:
  338. // result:=is_ref_addr16(ref);
  339. // OT_REF_BC:
  340. // result:=is_ref_bc(ref);
  341. // OT_REF_DE:
  342. // result:=is_ref_de(ref);
  343. // OT_REF_HL:
  344. // result:=is_ref_hl(ref);
  345. // OT_REF_SP:
  346. // result:=is_ref_sp(ref);
  347. // OT_REF_IX:
  348. // result:=is_ref_ix(ref);
  349. // OT_REF_IY:
  350. // result:=is_ref_iy(ref);
  351. // OT_REF_IX_d:
  352. // result:=is_ref_ix_d(ref);
  353. // OT_REF_IY_d:
  354. // result:=is_ref_iy_d(ref);
  355. // else
  356. // internalerror(2020041801);
  357. // end;
  358. // end;
  359. //
  360. //
  361. //function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  362. // var
  363. // ot: trefoperandtype;
  364. // begin
  365. // result:=true;
  366. // for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  367. // if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  368. // exit;
  369. // result:=false;
  370. // end;
  371. {****************************************************************************
  372. Instruction table
  373. *****************************************************************************}
  374. procedure BuildInsTabCache;
  375. var
  376. i : longint;
  377. begin
  378. new(instabcache);
  379. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  380. i:=0;
  381. while (i<InsTabEntries) do
  382. begin
  383. if InsTabCache^[InsTab[i].OPcode]=-1 then
  384. InsTabCache^[InsTab[i].OPcode]:=i;
  385. inc(i);
  386. end;
  387. end;
  388. procedure InitAsm;
  389. begin
  390. if not assigned(instabcache) then
  391. BuildInsTabCache;
  392. end;
  393. procedure DoneAsm;
  394. begin
  395. if assigned(instabcache) then
  396. begin
  397. dispose(instabcache);
  398. instabcache:=nil;
  399. end;
  400. end;
  401. begin
  402. cai_cpu:=taicpu;
  403. cai_align:=tai_align;
  404. end.