aasmcpu.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. {
  2. Copyright (c) 1999-2008 by Mazen Neifer and Florian Klaempfl
  3. Contains the assembler object for the Z80
  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 z80nop.inc}
  32. maxinfolen = 18;
  33. type
  34. { Operand types }
  35. toperandtype=(
  36. OT_NONE,
  37. OT_IMM3, { 3-bit immediate value (bit number: [0..7]) }
  38. OT_IMM8, { 8-bit immediate value }
  39. OT_IMM16, { 16-bit immediate value }
  40. OT_IMM_VAL0, { the immediate value 0 }
  41. OT_IMM_VAL1, { the immediate value 1 }
  42. OT_IMM_VAL2, { the immediate value 2 }
  43. OT_IMM_RST, { immediate value in [$00,$08,$10,$18,$20,$28,$30,$38] }
  44. OT_IMM_PORT, { 8-bit immediate port number for the IN and OUT instructions }
  45. OT_REG8, { 8-bit register: A/B/C/D/E/H/L }
  46. OT_REG8_A, { register A }
  47. OT_REG8_I, { register I }
  48. OT_REG8_R, { register R }
  49. OT_REG8_C_PORT, { implied parameter of the IN and OUT instructions }
  50. OT_REG16_IX, { register IX }
  51. OT_REG16_IY, { register IY }
  52. OT_REG16_SP, { register SP }
  53. OT_REG16_BC_DE_HL_SP, { 16-bit register pair: BC/DE/HL/SP }
  54. OT_REG16_BC_DE_HL_AF, { 16-bit register pair: BC/DE/HL/AF }
  55. OT_REG16_BC_DE_IX_SP, { 16-bit register pair: BC/DE/IX/SP }
  56. OT_REG16_BC_DE_IY_SP, { 16-bit register pair: BC/DE/IY/SP }
  57. OT_REG16_DE, { 16-bit register pair DE }
  58. OT_REG16_HL, { 16-bit register pair HL }
  59. OT_REG16_AF, { 16-bit register pair AF }
  60. OT_REG16_AF_, { alternate register set, 16-bit register pair AF' }
  61. OT_RELJMP8, { 8-bit relative jump offset }
  62. OT_COND, { condition: NZ/Z/NC/C/PO/PE/P/M }
  63. OT_COND_C, { condition C }
  64. OT_COND_NC, { condition NC }
  65. OT_COND_Z, { condition Z }
  66. OT_COND_NZ, { condition NZ }
  67. OT_REF_ADDR16, { memory contents at address (nn = 16-bit immediate address) }
  68. OT_REF_BC, { memory contents at address in register BC }
  69. OT_REF_DE, { memory contents at address in register DE }
  70. OT_REF_HL, { memory contents at address in register HL }
  71. OT_REF_SP, { memory contents at address in register SP }
  72. OT_REF_IX, { memory contents at address in register IX }
  73. OT_REF_IY, { memory contents at address in register IY }
  74. OT_REF_IX_d, { memory contents at address in register IX+d, d is in [-128..127] }
  75. OT_REF_IY_d); { memory contents at address in register IY+d, d is in [-128..127] }
  76. timmoperandtype = OT_IMM3..OT_IMM_PORT;
  77. tregoperandtype = OT_REG8..OT_REG16_AF_;
  78. treg8operandtype = OT_REG8..OT_REG8_C_PORT;
  79. treg16operandtype = OT_REG16_IX..OT_REG16_AF_;
  80. tcondoperandtype = OT_COND..OT_COND_NZ;
  81. trefoperandtype = OT_REF_ADDR16..OT_REF_IY_d;
  82. trefoperandtypes = set of trefoperandtype;
  83. tinsentry = record
  84. opcode : tasmop;
  85. ops : byte;
  86. optypes : array[0..max_operands-1] of toperandtype;
  87. code : array[0..maxinfolen] of char;
  88. flags : longint;
  89. end;
  90. pinsentry=^tinsentry;
  91. { taicpu }
  92. taicpu = class(tai_cpu_abstract_sym)
  93. private
  94. { next fields are filled in pass1, so pass2 is faster }
  95. insentry : PInsEntry;
  96. inssize : shortint;
  97. function Matches(p:PInsEntry):boolean;
  98. function FindInsentry(objdata:TObjData):boolean;
  99. public
  100. constructor op_none(op : tasmop);
  101. constructor op_reg(op : tasmop;_op1 : tregister);
  102. constructor op_const(op : tasmop;_op1 : LongInt);
  103. constructor op_ref(op : tasmop;const _op1 : treference);
  104. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  105. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  106. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  107. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  108. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  109. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  110. { this is for Jmp instructions }
  111. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  112. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  113. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  114. procedure loadbool(opidx:longint;_b:boolean);
  115. { register allocation }
  116. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  117. { register spilling code }
  118. function spilling_get_operation_type(opnr: longint): topertype;override;
  119. function CheckIfValid:boolean;
  120. function GetString:string;
  121. end;
  122. tai_align = class(tai_align_abstract)
  123. { nothing to add }
  124. end;
  125. procedure InitAsm;
  126. procedure DoneAsm;
  127. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  128. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  129. function is_ref_addr16(const ref:treference): Boolean;
  130. function is_ref_bc(const ref:treference): Boolean;
  131. function is_ref_de(const ref:treference): Boolean;
  132. function is_ref_hl(const ref:treference): Boolean;
  133. function is_ref_sp(const ref:treference): Boolean;
  134. function is_ref_ix(const ref:treference): Boolean;
  135. function is_ref_iy(const ref:treference): Boolean;
  136. function is_ref_ix_d(const ref:treference): Boolean;
  137. function is_ref_iy_d(const ref:treference): Boolean;
  138. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  139. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  140. implementation
  141. {****************************************************************************
  142. Instruction table
  143. *****************************************************************************}
  144. type
  145. TInsTabCache=array[TasmOp] of longint;
  146. PInsTabCache=^TInsTabCache;
  147. const
  148. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  149. var
  150. InsTabCache : PInsTabCache;
  151. {*****************************************************************************
  152. taicpu Constructors
  153. *****************************************************************************}
  154. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  155. begin
  156. if opidx>=ops then
  157. ops:=opidx+1;
  158. with oper[opidx]^ do
  159. begin
  160. if typ=top_ref then
  161. dispose(ref);
  162. b:=_b;
  163. typ:=top_bool;
  164. end;
  165. end;
  166. function taicpu.Matches(p: PInsEntry): boolean;
  167. function OperandsMatch(const oper: toper; const ot: toperandtype): boolean;
  168. begin
  169. case ot of
  170. OT_IMM3:
  171. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=7);
  172. OT_IMM8:
  173. { todo: lo8, hi8 addresses }
  174. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=255);
  175. OT_IMM16:
  176. { todo: addresses }
  177. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=65535);
  178. OT_IMM_VAL0:
  179. result:=(oper.typ=top_const) and (oper.val=0);
  180. OT_IMM_VAL1:
  181. result:=(oper.typ=top_const) and (oper.val=1);
  182. OT_IMM_VAL2:
  183. result:=(oper.typ=top_const) and (oper.val=2);
  184. OT_IMM_RST:
  185. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  186. (oper.val=$10) or (oper.val=$18) or
  187. (oper.val=$20) or (oper.val=$28) or
  188. (oper.val=$30) or (oper.val=$38));
  189. OT_IMM_PORT:
  190. result:=(oper.typ=top_ref) and
  191. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  192. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  193. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  194. OT_REG8:
  195. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  196. (oper.reg=NR_C) or (oper.reg=NR_D) or
  197. (oper.reg=NR_E) or (oper.reg=NR_H) or
  198. (oper.reg=NR_L));
  199. OT_REG8_A:
  200. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  201. OT_REG8_I:
  202. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  203. OT_REG8_R:
  204. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  205. {todo: OT_REG8_C_PORT}
  206. OT_REG16_IX:
  207. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  208. OT_REG16_IY:
  209. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  210. OT_REG16_SP:
  211. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  212. OT_REG16_BC_DE_HL_SP:
  213. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_HL) or (oper.reg=NR_SP));
  214. OT_REG16_BC_DE_HL_AF:
  215. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_HL) or (oper.reg=NR_AF));
  216. OT_REG16_BC_DE_IX_SP:
  217. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_IX) or (oper.reg=NR_SP));
  218. OT_REG16_BC_DE_IY_SP:
  219. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_IY) or (oper.reg=NR_SP));
  220. OT_REG16_DE:
  221. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  222. OT_REG16_HL:
  223. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  224. OT_REG16_AF:
  225. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  226. OT_REG16_AF_:
  227. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  228. {todo: OT_RELJMP8}
  229. OT_REF_ADDR16,
  230. OT_REF_BC,
  231. OT_REF_DE,
  232. OT_REF_HL,
  233. OT_REF_SP,
  234. OT_REF_IX,
  235. OT_REF_IY,
  236. OT_REF_IX_d,
  237. OT_REF_IY_d:
  238. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  239. else
  240. internalerror(2020042901);
  241. end;
  242. end;
  243. var
  244. i: Integer;
  245. begin
  246. result:=false;
  247. { Check the opcode }
  248. if p^.opcode<>opcode then
  249. exit;
  250. { The opcode doesn't support conditions, but we have a condition?
  251. That's an invalid instruction, don't match it against anything. }
  252. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  253. exit;
  254. { if our opcode supports a condition, but our operation doesn't have
  255. one, and we're matching it with an instruction entry 'p' that has a
  256. condition, then it doesn't match }
  257. if (opcode in cond_instructions) and (condition=C_None) and
  258. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  259. exit;
  260. { instruction has a condition? }
  261. if (opcode in cond_instructions) and (condition<>C_None) then
  262. begin
  263. { Check the operand count }
  264. if p^.ops<>(ops+1) then
  265. exit;
  266. { Check the condition }
  267. case p^.optypes[0] of
  268. OT_COND:
  269. { any condition accepted };
  270. OT_COND_C:
  271. if condition<>C_C then
  272. exit;
  273. OT_COND_NC:
  274. if condition<>C_NC then
  275. exit;
  276. OT_COND_Z:
  277. if condition<>C_Z then
  278. exit;
  279. OT_COND_NZ:
  280. if condition<>C_NZ then
  281. exit;
  282. else
  283. { no condition in 'p'? Then it's not a match! }
  284. exit;
  285. end;
  286. { Check the operands }
  287. for i:=1 to p^.ops-1 do
  288. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  289. exit;
  290. end
  291. else
  292. { no condition }
  293. begin
  294. { Check the operand count }
  295. if p^.ops<>ops then
  296. exit;
  297. { Check the operands }
  298. for i:=0 to p^.ops-1 do
  299. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  300. exit;
  301. end;
  302. result:=true;
  303. end;
  304. function taicpu.FindInsentry(objdata: TObjData): boolean;
  305. var
  306. i : longint;
  307. begin
  308. result:=false;
  309. { Things which may only be done once, not when a second pass is done to
  310. optimize }
  311. if (Insentry=nil) {or (IF_PASS2 in InsEntry^.flags)} then
  312. begin
  313. { set the file postion }
  314. current_filepos:=fileinfo;
  315. end
  316. else
  317. begin
  318. { we've already an insentry so it's valid }
  319. result:=true;
  320. exit;
  321. end;
  322. { Lookup opcode in the table }
  323. InsSize:=-1;
  324. i:=instabcache^[opcode];
  325. if i=-1 then
  326. begin
  327. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  328. exit;
  329. end;
  330. insentry:=@instab[i];
  331. while (insentry^.opcode=opcode) do
  332. begin
  333. if matches(insentry) then
  334. begin
  335. result:=true;
  336. exit;
  337. end;
  338. inc(insentry);
  339. end;
  340. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  341. { No instruction found, set insentry to nil and inssize to -1 }
  342. insentry:=nil;
  343. inssize:=-1;
  344. end;
  345. constructor taicpu.op_none(op : tasmop);
  346. begin
  347. inherited create(op);
  348. end;
  349. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  350. begin
  351. inherited create(op);
  352. ops:=1;
  353. loadreg(0,_op1);
  354. end;
  355. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  356. begin
  357. inherited create(op);
  358. ops:=1;
  359. loadref(0,_op1);
  360. end;
  361. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  362. begin
  363. inherited create(op);
  364. ops:=1;
  365. loadconst(0,_op1);
  366. end;
  367. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  368. begin
  369. inherited create(op);
  370. ops:=2;
  371. loadreg(0,_op1);
  372. loadreg(1,_op2);
  373. end;
  374. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  375. begin
  376. inherited create(op);
  377. ops:=2;
  378. loadreg(0,_op1);
  379. loadconst(1,_op2);
  380. end;
  381. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  382. begin
  383. inherited create(op);
  384. ops:=2;
  385. loadconst(0,_op1);
  386. loadreg(1,_op2);
  387. end;
  388. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  389. begin
  390. inherited create(op);
  391. ops:=2;
  392. loadreg(0,_op1);
  393. loadref(1,_op2);
  394. end;
  395. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  396. begin
  397. inherited create(op);
  398. ops:=2;
  399. loadref(0,_op1);
  400. loadreg(1,_op2);
  401. end;
  402. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  403. begin
  404. inherited create(op);
  405. ops:=2;
  406. loadref(0,_op1);
  407. loadconst(1,_op2);
  408. end;
  409. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  410. begin
  411. inherited create(op);
  412. is_jmp:=op in jmp_instructions;
  413. condition:=cond;
  414. ops:=1;
  415. loadsymbol(0,_op1,0);
  416. end;
  417. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  418. begin
  419. inherited create(op);
  420. is_jmp:=op in jmp_instructions;
  421. ops:=1;
  422. loadsymbol(0,_op1,0);
  423. end;
  424. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  425. begin
  426. inherited create(op);
  427. ops:=1;
  428. loadsymbol(0,_op1,_op1ofs);
  429. end;
  430. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  431. begin
  432. result:=(
  433. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  434. ) and
  435. (ops=2) and
  436. (oper[0]^.typ=top_reg) and
  437. (oper[1]^.typ=top_reg) and
  438. (oper[0]^.reg=oper[1]^.reg);
  439. end;
  440. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  441. begin
  442. result:=operand_read;
  443. case opcode of
  444. A_LD,
  445. A_POP:
  446. if opnr=0 then
  447. result:=operand_write;
  448. A_PUSH,
  449. A_BIT,
  450. A_CP,
  451. A_DJNZ,
  452. A_JR,
  453. A_JP,
  454. A_CALL,
  455. A_RET,
  456. A_RETI,
  457. A_RETN,
  458. A_RST,
  459. A_IM:
  460. ;
  461. A_SET,
  462. A_RES:
  463. if opnr=1 then
  464. result:=operand_readwrite;
  465. A_EX:
  466. result:=operand_readwrite;
  467. else
  468. begin
  469. if opnr=0 then
  470. result:=operand_readwrite;
  471. end;
  472. end;
  473. end;
  474. function taicpu.CheckIfValid: boolean;
  475. begin
  476. result:=FindInsEntry(nil);
  477. end;
  478. function taicpu.GetString: string;
  479. var
  480. //i : longint;
  481. s : string;
  482. //regnr: string;
  483. //addsize : boolean;
  484. begin
  485. s:='['+std_op2str[opcode];
  486. //for i:=0 to ops-1 do
  487. // begin
  488. // with oper[i]^ do
  489. // begin
  490. // if i=0 then
  491. // s:=s+' '
  492. // else
  493. // s:=s+',';
  494. // { type }
  495. // addsize:=false;
  496. //
  497. // regnr := '';
  498. // if getregtype(reg) = R_MMREGISTER then
  499. // str(getsupreg(reg),regnr);
  500. //
  501. // if (ot and OT_XMMREG)=OT_XMMREG then
  502. // s:=s+'xmmreg' + regnr
  503. // else
  504. // if (ot and OT_YMMREG)=OT_YMMREG then
  505. // s:=s+'ymmreg' + regnr
  506. // else
  507. // if (ot and OT_ZMMREG)=OT_ZMMREG then
  508. // s:=s+'zmmreg' + regnr
  509. //
  510. // else
  511. // if (ot and OT_REG_EXTRA_MASK)=OT_MMXREG then
  512. // s:=s+'mmxreg'
  513. // else
  514. // if (ot and OT_REG_EXTRA_MASK)=OT_FPUREG then
  515. // s:=s+'fpureg'
  516. // else
  517. // if (ot and OT_REGISTER)=OT_REGISTER then
  518. // begin
  519. // s:=s+'reg';
  520. // addsize:=true;
  521. // end
  522. // else
  523. // if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  524. // begin
  525. // s:=s+'imm';
  526. // addsize:=true;
  527. // end
  528. // else
  529. // if (ot and OT_MEMORY)=OT_MEMORY then
  530. // begin
  531. // s:=s+'mem';
  532. // addsize:=true;
  533. // end
  534. // else
  535. // s:=s+'???';
  536. // { size }
  537. // if addsize then
  538. // begin
  539. // if (ot and OT_BITS8)<>0 then
  540. // s:=s+'8'
  541. // else
  542. // if (ot and OT_BITS16)<>0 then
  543. // s:=s+'16'
  544. // else
  545. // if (ot and OT_BITS32)<>0 then
  546. // s:=s+'32'
  547. // else
  548. // if (ot and OT_BITS64)<>0 then
  549. // s:=s+'64'
  550. // else
  551. // if (ot and OT_BITS128)<>0 then
  552. // s:=s+'128'
  553. // else
  554. // if (ot and OT_BITS256)<>0 then
  555. // s:=s+'256'
  556. // else
  557. // if (ot and OT_BITS512)<>0 then
  558. // s:=s+'512'
  559. // else
  560. // s:=s+'??';
  561. // { signed }
  562. // if (ot and OT_SIGNED)<>0 then
  563. // s:=s+'s';
  564. // end;
  565. //
  566. // if vopext <> 0 then
  567. // begin
  568. // str(vopext and $07, regnr);
  569. // if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  570. // s := s + ' {k' + regnr + '}';
  571. //
  572. // if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  573. // s := s + ' {z}';
  574. //
  575. // if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  576. // s := s + ' {sae}';
  577. //
  578. //
  579. // if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  580. // case vopext and OTVE_VECTOR_BCST_MASK of
  581. // OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  582. // OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  583. // OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  584. // OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  585. // end;
  586. //
  587. // if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  588. // case vopext and OTVE_VECTOR_ER_MASK of
  589. // OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  590. // OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  591. // OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  592. // OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  593. // end;
  594. //
  595. // end;
  596. // end;
  597. // end;
  598. GetString:=s+']';
  599. end;
  600. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  601. begin
  602. case getregtype(r) of
  603. R_INTREGISTER :
  604. result:=taicpu.op_reg_ref(A_LD,r,ref)
  605. else
  606. internalerror(200401041);
  607. end;
  608. end;
  609. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  610. begin
  611. case getregtype(r) of
  612. R_INTREGISTER :
  613. result:=taicpu.op_ref_reg(A_LD,ref,r);
  614. else
  615. internalerror(200401041);
  616. end;
  617. end;
  618. function is_ref_addr16(const ref: treference): Boolean;
  619. begin
  620. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  621. end;
  622. function is_ref_bc(const ref: treference): Boolean;
  623. begin
  624. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  625. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  626. (ref.offset=0) and (ref.scalefactor<=1) and
  627. (ref.symbol=nil) and (ref.relsymbol=nil);
  628. end;
  629. function is_ref_de(const ref: treference): Boolean;
  630. begin
  631. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  632. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  633. (ref.offset=0) and (ref.scalefactor<=1) and
  634. (ref.symbol=nil) and (ref.relsymbol=nil);
  635. end;
  636. function is_ref_hl(const ref: treference): Boolean;
  637. begin
  638. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  639. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  640. (ref.offset=0) and (ref.scalefactor<=1) and
  641. (ref.symbol=nil) and (ref.relsymbol=nil);
  642. end;
  643. function is_ref_sp(const ref: treference): Boolean;
  644. begin
  645. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  646. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  647. (ref.offset=0) and (ref.scalefactor<=1) and
  648. (ref.symbol=nil) and (ref.relsymbol=nil);
  649. end;
  650. function is_ref_ix(const ref: treference): Boolean;
  651. begin
  652. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  653. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  654. (ref.offset=0) and (ref.scalefactor<=1) and
  655. (ref.symbol=nil) and (ref.relsymbol=nil);
  656. end;
  657. function is_ref_iy(const ref: treference): Boolean;
  658. begin
  659. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  660. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  661. (ref.offset=0) and (ref.scalefactor<=1) and
  662. (ref.symbol=nil) and (ref.relsymbol=nil);
  663. end;
  664. function is_ref_ix_d(const ref: treference): Boolean;
  665. begin
  666. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  667. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  668. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  669. (ref.symbol=nil) and (ref.relsymbol=nil);
  670. end;
  671. function is_ref_iy_d(const ref: treference): Boolean;
  672. begin
  673. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  674. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  675. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  676. (ref.symbol=nil) and (ref.relsymbol=nil);
  677. end;
  678. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  679. begin
  680. case opertype of
  681. OT_REF_ADDR16:
  682. result:=is_ref_addr16(ref);
  683. OT_REF_BC:
  684. result:=is_ref_bc(ref);
  685. OT_REF_DE:
  686. result:=is_ref_de(ref);
  687. OT_REF_HL:
  688. result:=is_ref_hl(ref);
  689. OT_REF_SP:
  690. result:=is_ref_sp(ref);
  691. OT_REF_IX:
  692. result:=is_ref_ix(ref);
  693. OT_REF_IY:
  694. result:=is_ref_iy(ref);
  695. OT_REF_IX_d:
  696. result:=is_ref_ix_d(ref);
  697. OT_REF_IY_d:
  698. result:=is_ref_iy_d(ref);
  699. else
  700. internalerror(2020041801);
  701. end;
  702. end;
  703. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  704. var
  705. ot: trefoperandtype;
  706. begin
  707. result:=true;
  708. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  709. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  710. exit;
  711. result:=false;
  712. end;
  713. {****************************************************************************
  714. Instruction table
  715. *****************************************************************************}
  716. procedure BuildInsTabCache;
  717. var
  718. i : longint;
  719. begin
  720. new(instabcache);
  721. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  722. i:=0;
  723. while (i<InsTabEntries) do
  724. begin
  725. if InsTabCache^[InsTab[i].OPcode]=-1 then
  726. InsTabCache^[InsTab[i].OPcode]:=i;
  727. inc(i);
  728. end;
  729. end;
  730. procedure InitAsm;
  731. begin
  732. if not assigned(instabcache) then
  733. BuildInsTabCache;
  734. end;
  735. procedure DoneAsm;
  736. begin
  737. if assigned(instabcache) then
  738. begin
  739. dispose(instabcache);
  740. instabcache:=nil;
  741. end;
  742. end;
  743. begin
  744. cai_cpu:=taicpu;
  745. cai_align:=tai_align;
  746. end.