aasmcpu.pas 29 KB

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