aasmcpu.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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. insoffset : longint;
  98. LastInsOffset : longint;
  99. function Matches(p:PInsEntry;objdata:TObjData):boolean;
  100. function FindInsentry(objdata:TObjData):boolean;
  101. function calcsize(p:PInsEntry):shortint;
  102. procedure gencode(objdata:TObjData);
  103. procedure init; { this need to be called by all constructors }
  104. public
  105. constructor op_none(op : tasmop);
  106. constructor op_reg(op : tasmop;_op1 : tregister);
  107. constructor op_const(op : tasmop;_op1 : LongInt);
  108. constructor op_ref(op : tasmop;const _op1 : treference);
  109. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  110. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  111. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  112. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  113. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  114. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  115. { this is for Jmp instructions }
  116. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  117. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  118. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  119. procedure loadbool(opidx:longint;_b:boolean);
  120. { register allocation }
  121. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  122. { register spilling code }
  123. function spilling_get_operation_type(opnr: longint): topertype;override;
  124. procedure ResetPass1;override;
  125. procedure ResetPass2;override;
  126. function Pass1(objdata:TObjData):longint;override;
  127. procedure Pass2(objdata:TObjData);override;
  128. function CheckIfValid:boolean;
  129. function GetString:string;
  130. end;
  131. tai_align = class(tai_align_abstract)
  132. { nothing to add }
  133. end;
  134. procedure InitAsm;
  135. procedure DoneAsm;
  136. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  137. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  138. function is_ref_addr16(const ref:treference): Boolean;
  139. function is_ref_bc(const ref:treference): Boolean;
  140. function is_ref_de(const ref:treference): Boolean;
  141. function is_ref_hl(const ref:treference): Boolean;
  142. function is_ref_sp(const ref:treference): Boolean;
  143. function is_ref_ix(const ref:treference): Boolean;
  144. function is_ref_iy(const ref:treference): Boolean;
  145. function is_ref_ix_d(const ref:treference): Boolean;
  146. function is_ref_iy_d(const ref:treference): Boolean;
  147. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  148. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  149. implementation
  150. {****************************************************************************
  151. Instruction table
  152. *****************************************************************************}
  153. type
  154. TInsTabCache=array[TasmOp] of longint;
  155. PInsTabCache=^TInsTabCache;
  156. const
  157. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  158. var
  159. InsTabCache : PInsTabCache;
  160. {*****************************************************************************
  161. taicpu Constructors
  162. *****************************************************************************}
  163. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  164. begin
  165. if opidx>=ops then
  166. ops:=opidx+1;
  167. with oper[opidx]^ do
  168. begin
  169. if typ=top_ref then
  170. dispose(ref);
  171. b:=_b;
  172. typ:=top_bool;
  173. end;
  174. end;
  175. function taicpu.Matches(p: PInsEntry; objdata:TObjData): boolean;
  176. function OperandsMatch(const oper: toper; const ot: toperandtype): boolean;
  177. var
  178. currsym: TObjSymbol;
  179. l: ASizeInt;
  180. relsize: LongInt;
  181. begin
  182. case ot of
  183. OT_IMM3:
  184. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=7);
  185. OT_IMM8:
  186. result:=((oper.typ=top_const) and (oper.val>=-128) and (oper.val<=255)) or
  187. ((oper.typ=top_ref) and
  188. (oper.ref^.refaddr in [addr_hi8,addr_lo8]) and assigned(oper.ref^.symbol) and
  189. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  190. OT_IMM16:
  191. result:=((oper.typ=top_const) and (oper.val>=-32768) and (oper.val<=65535)) or
  192. ((oper.typ=top_ref) and
  193. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  194. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  195. OT_IMM_VAL0:
  196. result:=(oper.typ=top_const) and (oper.val=0);
  197. OT_IMM_VAL1:
  198. result:=(oper.typ=top_const) and (oper.val=1);
  199. OT_IMM_VAL2:
  200. result:=(oper.typ=top_const) and (oper.val=2);
  201. OT_IMM_RST:
  202. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  203. (oper.val=$10) or (oper.val=$18) or
  204. (oper.val=$20) or (oper.val=$28) or
  205. (oper.val=$30) or (oper.val=$38));
  206. OT_IMM_PORT:
  207. result:=(oper.typ=top_ref) and
  208. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  209. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  210. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  211. OT_REG8:
  212. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  213. (oper.reg=NR_C) or (oper.reg=NR_D) or
  214. (oper.reg=NR_E) or (oper.reg=NR_H) or
  215. (oper.reg=NR_L));
  216. OT_REG8_A:
  217. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  218. OT_REG8_I:
  219. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  220. OT_REG8_R:
  221. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  222. OT_REG8_C_PORT:
  223. result:=(oper.typ=top_ref) and
  224. (((oper.ref^.base=NR_C) and (oper.ref^.index=NR_NO)) or
  225. ((oper.ref^.base=NR_NO) and (oper.ref^.index=NR_C))) and
  226. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  227. (oper.ref^.offset=0);
  228. OT_REG16_IX:
  229. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  230. OT_REG16_IY:
  231. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  232. OT_REG16_SP:
  233. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  234. OT_REG16_BC_DE_HL_SP:
  235. 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));
  236. OT_REG16_BC_DE_HL_AF:
  237. 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));
  238. OT_REG16_BC_DE_IX_SP:
  239. 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));
  240. OT_REG16_BC_DE_IY_SP:
  241. 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));
  242. OT_REG16_DE:
  243. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  244. OT_REG16_HL:
  245. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  246. OT_REG16_AF:
  247. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  248. OT_REG16_AF_:
  249. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  250. OT_RELJMP8:
  251. begin
  252. result:=(oper.typ=top_ref) and
  253. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  254. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO);
  255. if result and assigned(objdata) then
  256. begin
  257. currsym:=objdata.symbolref(oper.ref^.symbol);
  258. l:=oper.ref^.offset;
  259. {$push}
  260. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  261. if assigned(currsym) then
  262. inc(l,currsym.address);
  263. {$pop}
  264. { when it is a forward jump we need to compensate the
  265. offset of the instruction since the previous time,
  266. because the symbol address is then still using the
  267. 'old-style' addressing.
  268. For backwards jumps this is not required because the
  269. address of the symbol is already adjusted to the
  270. new offset }
  271. if (l>InsOffset) and (LastInsOffset<>-1) then
  272. inc(l,InsOffset-LastInsOffset-1);
  273. { instruction size will then always become 2 (PFV) }
  274. relsize:=l-(InsOffset+2);
  275. result:=(relsize>=-128) and (relsize<=127) and
  276. (
  277. not assigned(currsym) or
  278. (currsym.objsection=objdata.currobjsec)
  279. );
  280. end;
  281. end;
  282. OT_REF_ADDR16,
  283. OT_REF_BC,
  284. OT_REF_DE,
  285. OT_REF_HL,
  286. OT_REF_SP,
  287. OT_REF_IX,
  288. OT_REF_IY,
  289. OT_REF_IX_d,
  290. OT_REF_IY_d:
  291. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  292. else
  293. internalerror(2020042901);
  294. end;
  295. end;
  296. var
  297. i: Integer;
  298. begin
  299. result:=false;
  300. { Check the opcode }
  301. if p^.opcode<>opcode then
  302. exit;
  303. { The opcode doesn't support conditions, but we have a condition?
  304. That's an invalid instruction, don't match it against anything. }
  305. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  306. exit;
  307. { if our opcode supports a condition, but our operation doesn't have
  308. one, and we're matching it with an instruction entry 'p' that has a
  309. condition, then it doesn't match }
  310. if (opcode in cond_instructions) and (condition=C_None) and
  311. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  312. exit;
  313. { instruction has a condition? }
  314. if (opcode in cond_instructions) and (condition<>C_None) then
  315. begin
  316. { Check the operand count }
  317. if p^.ops<>(ops+1) then
  318. exit;
  319. { Check the condition }
  320. case p^.optypes[0] of
  321. OT_COND:
  322. { any condition accepted };
  323. OT_COND_C:
  324. if condition<>C_C then
  325. exit;
  326. OT_COND_NC:
  327. if condition<>C_NC then
  328. exit;
  329. OT_COND_Z:
  330. if condition<>C_Z then
  331. exit;
  332. OT_COND_NZ:
  333. if condition<>C_NZ then
  334. exit;
  335. else
  336. { no condition in 'p'? Then it's not a match! }
  337. exit;
  338. end;
  339. { Check the operands }
  340. for i:=1 to p^.ops-1 do
  341. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  342. exit;
  343. end
  344. else
  345. { no condition }
  346. begin
  347. { Check the operand count }
  348. if p^.ops<>ops then
  349. exit;
  350. { Check the operands }
  351. for i:=0 to p^.ops-1 do
  352. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  353. exit;
  354. end;
  355. result:=true;
  356. end;
  357. function taicpu.FindInsentry(objdata: TObjData): boolean;
  358. var
  359. i : longint;
  360. begin
  361. result:=false;
  362. { Things which may only be done once, not when a second pass is done to
  363. optimize }
  364. if (Insentry=nil) or (opcode=A_JRJP) then
  365. begin
  366. { set the file postion }
  367. current_filepos:=fileinfo;
  368. end
  369. else
  370. begin
  371. { we've already an insentry so it's valid }
  372. result:=true;
  373. exit;
  374. end;
  375. { Lookup opcode in the table }
  376. InsSize:=-1;
  377. i:=instabcache^[opcode];
  378. if i=-1 then
  379. begin
  380. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  381. exit;
  382. end;
  383. insentry:=@instab[i];
  384. while (insentry^.opcode=opcode) do
  385. begin
  386. if matches(insentry,objdata) then
  387. begin
  388. result:=true;
  389. exit;
  390. end;
  391. inc(insentry);
  392. end;
  393. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  394. { No instruction found, set insentry to nil and inssize to -1 }
  395. insentry:=nil;
  396. inssize:=-1;
  397. end;
  398. function taicpu.calcsize(p: PInsEntry): shortint;
  399. var
  400. code, token: string;
  401. i: Integer;
  402. ch: Char;
  403. begin
  404. result:=0;
  405. code:=insentry^.code;
  406. i:=1;
  407. token:='';
  408. while i<=length(code) do
  409. begin
  410. ch:=code[i];
  411. Inc(i);
  412. if ch<>',' then
  413. token:=token+ch;
  414. if (ch=',') or (i>length(code)) then
  415. begin
  416. if token='' then
  417. internalerror(2020050402);
  418. if (token[1]='$') or (token[1]='%') or (token='n') or (token='d') or (token='e') then
  419. Inc(result)
  420. else if token='nn' then
  421. Inc(result,2)
  422. else
  423. internalerror(2020050504);
  424. token:='';
  425. end;
  426. end;
  427. end;
  428. procedure taicpu.gencode(objdata: TObjData);
  429. procedure WriteByte(b: byte);
  430. begin
  431. objdata.writebytes(b,1);
  432. end;
  433. procedure WriteWord(w: word);
  434. var
  435. bytes: array [0..1] of Byte;
  436. begin
  437. bytes[0]:=Byte(w);
  438. bytes[1]:=Byte(w shr 8);
  439. objdata.writebytes(bytes,2);
  440. end;
  441. procedure WriteNN;
  442. var
  443. i, j: Integer;
  444. begin
  445. for j:=0 to insentry^.ops-1 do
  446. begin
  447. if condition=C_NONE then
  448. i:=j
  449. else
  450. i:=j-1;
  451. if insentry^.optypes[j]=OT_IMM16 then
  452. begin
  453. case oper[i]^.typ of
  454. top_const:
  455. begin
  456. WriteWord(Word(oper[i]^.val));
  457. exit;
  458. end;
  459. top_ref:
  460. begin
  461. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  462. internalerror(2020050406);
  463. if Assigned(oper[i]^.ref^.symbol) then
  464. begin
  465. if oper[i]^.ref^.refaddr<>addr_full then
  466. internalerror(2020050407);
  467. objdata.writeReloc(oper[i]^.ref^.offset,2,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE);
  468. exit;
  469. end
  470. else
  471. begin
  472. WriteWord(oper[i]^.ref^.offset);
  473. exit;
  474. end;
  475. end;
  476. else
  477. InternalError(2020050404);
  478. end;
  479. end
  480. else if insentry^.optypes[j]=OT_REF_ADDR16 then
  481. begin
  482. case oper[i]^.typ of
  483. top_ref:
  484. begin
  485. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  486. internalerror(2020050603);
  487. if Assigned(oper[i]^.ref^.symbol) then
  488. begin
  489. if oper[i]^.ref^.refaddr<>addr_no then
  490. internalerror(2020050604);
  491. objdata.writeReloc(oper[i]^.ref^.offset,2,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE);
  492. exit;
  493. end
  494. else
  495. begin
  496. WriteWord(oper[i]^.ref^.offset);
  497. exit;
  498. end;
  499. end;
  500. else
  501. InternalError(2020050602);
  502. end;
  503. end;
  504. end;
  505. InternalError(2020050403);
  506. end;
  507. procedure WriteN;
  508. var
  509. i: Integer;
  510. begin
  511. for i:=0 to insentry^.ops-1 do
  512. begin
  513. if insentry^.optypes[i]=OT_IMM8 then
  514. begin
  515. case oper[i]^.typ of
  516. top_const:
  517. begin
  518. WriteByte(Byte(oper[i]^.val));
  519. exit;
  520. end;
  521. top_ref:
  522. begin
  523. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  524. internalerror(2020050507);
  525. if Assigned(oper[i]^.ref^.symbol) then
  526. begin
  527. case oper[i]^.ref^.refaddr of
  528. addr_hi8:
  529. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_HI8);
  530. addr_lo8:
  531. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_LO8);
  532. else
  533. internalerror(2020050408);
  534. end;
  535. exit;
  536. end
  537. else
  538. internalerror(2020050409);
  539. end;
  540. else
  541. InternalError(2020050506);
  542. end;
  543. end
  544. else if insentry^.optypes[i]=OT_IMM_PORT then
  545. begin
  546. case oper[i]^.typ of
  547. top_ref:
  548. begin
  549. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) or Assigned(oper[i]^.ref^.symbol) then
  550. internalerror(2020050612);
  551. WriteByte(Byte(oper[i]^.ref^.offset));
  552. exit;
  553. end;
  554. else
  555. InternalError(2020050611);
  556. end;
  557. end;
  558. end;
  559. InternalError(2020050505);
  560. end;
  561. procedure WriteD;
  562. var
  563. i: Integer;
  564. begin
  565. for i:=0 to insentry^.ops-1 do
  566. begin
  567. if insentry^.optypes[i] in [OT_REF_IX_d,OT_REF_IY_d] then
  568. begin
  569. case oper[i]^.typ of
  570. top_ref:
  571. begin
  572. if not is_ref_opertype(oper[i]^.ref^,insentry^.optypes[i]) then
  573. internalerror(2020050510);
  574. WriteByte(Byte(oper[i]^.ref^.offset));
  575. exit;
  576. end;
  577. else
  578. InternalError(2020050511);
  579. end;
  580. end;
  581. end;
  582. InternalError(2020050512);
  583. end;
  584. procedure WriteE;
  585. var
  586. i, j: Integer;
  587. begin
  588. for j:=0 to insentry^.ops-1 do
  589. begin
  590. if condition=C_NONE then
  591. i:=j
  592. else
  593. i:=j-1;
  594. if insentry^.optypes[j]=OT_RELJMP8 then
  595. begin
  596. case oper[i]^.typ of
  597. top_ref:
  598. begin
  599. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  600. internalerror(2020050608);
  601. if Assigned(oper[i]^.ref^.symbol) then
  602. begin
  603. if oper[i]^.ref^.refaddr<>addr_full then
  604. internalerror(2020050609);
  605. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_RELATIVE);
  606. exit;
  607. end
  608. else
  609. internalerror(2020050610);
  610. exit;
  611. end;
  612. else
  613. InternalError(2020050607);
  614. end;
  615. end;
  616. end;
  617. end;
  618. function EvalMaskCode(const maskcode: string): byte;
  619. var
  620. i: Integer;
  621. begin
  622. case maskcode of
  623. 'dd':
  624. for i:=0 to insentry^.ops-1 do
  625. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_SP then
  626. begin
  627. if oper[i]^.typ<>top_reg then
  628. internalerror(2020050410);
  629. case oper[i]^.reg of
  630. NR_BC:
  631. result:=0;
  632. NR_DE:
  633. result:=1;
  634. NR_HL:
  635. result:=2;
  636. NR_SP:
  637. result:=3;
  638. else
  639. internalerror(2020050411);
  640. end;
  641. end;
  642. 'qq':
  643. for i:=0 to insentry^.ops-1 do
  644. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_AF then
  645. begin
  646. if oper[i]^.typ<>top_reg then
  647. internalerror(2020050412);
  648. case oper[i]^.reg of
  649. NR_BC:
  650. result:=0;
  651. NR_DE:
  652. result:=1;
  653. NR_HL:
  654. result:=2;
  655. NR_AF:
  656. result:=3;
  657. else
  658. internalerror(2020050413);
  659. end;
  660. end;
  661. 'pp':
  662. for i:=0 to insentry^.ops-1 do
  663. if insentry^.optypes[i]=OT_REG16_BC_DE_IX_SP then
  664. begin
  665. if oper[i]^.typ<>top_reg then
  666. internalerror(2020050414);
  667. case oper[i]^.reg of
  668. NR_BC:
  669. result:=0;
  670. NR_DE:
  671. result:=1;
  672. NR_IX:
  673. result:=2;
  674. NR_SP:
  675. result:=3;
  676. else
  677. internalerror(2020050415);
  678. end;
  679. end;
  680. 'rr':
  681. for i:=0 to insentry^.ops-1 do
  682. if insentry^.optypes[i]=OT_REG16_BC_DE_IY_SP then
  683. begin
  684. if oper[i]^.typ<>top_reg then
  685. internalerror(2020050416);
  686. case oper[i]^.reg of
  687. NR_BC:
  688. result:=0;
  689. NR_DE:
  690. result:=1;
  691. NR_IY:
  692. result:=2;
  693. NR_SP:
  694. result:=3;
  695. else
  696. internalerror(2020050417);
  697. end;
  698. end;
  699. 'rrr':
  700. for i:=0 to insentry^.ops-1 do
  701. if insentry^.optypes[i]=OT_REG8 then
  702. begin
  703. if oper[i]^.typ<>top_reg then
  704. internalerror(2020050418);
  705. case oper[i]^.reg of
  706. NR_A:
  707. result:=7;
  708. NR_B:
  709. result:=0;
  710. NR_C:
  711. result:=1;
  712. NR_D:
  713. result:=2;
  714. NR_E:
  715. result:=3;
  716. NR_H:
  717. result:=4;
  718. NR_L:
  719. result:=5;
  720. else
  721. internalerror(2020050419);
  722. end;
  723. end;
  724. 'rrrRRR':
  725. begin
  726. if ops<>2 then
  727. internalerror(2020050420);
  728. if (insentry^.optypes[0]<>OT_REG8) or (insentry^.optypes[1]<>OT_REG8) then
  729. internalerror(2020050421);
  730. if (oper[0]^.typ<>top_reg) or (oper[1]^.typ<>top_reg) then
  731. internalerror(2020050422);
  732. case oper[0]^.reg of
  733. NR_A:
  734. result:=7 shl 3;
  735. NR_B:
  736. result:=0 shl 3;
  737. NR_C:
  738. result:=1 shl 3;
  739. NR_D:
  740. result:=2 shl 3;
  741. NR_E:
  742. result:=3 shl 3;
  743. NR_H:
  744. result:=4 shl 3;
  745. NR_L:
  746. result:=5 shl 3;
  747. else
  748. internalerror(2020050419);
  749. end;
  750. case oper[1]^.reg of
  751. NR_A:
  752. result:=result or 7;
  753. NR_B:
  754. result:=result or 0;
  755. NR_C:
  756. result:=result or 1;
  757. NR_D:
  758. result:=result or 2;
  759. NR_E:
  760. result:=result or 3;
  761. NR_H:
  762. result:=result or 4;
  763. NR_L:
  764. result:=result or 5;
  765. else
  766. internalerror(2020050419);
  767. end;
  768. end;
  769. 'ccc':
  770. begin
  771. case condition of
  772. C_NZ:
  773. result:=0;
  774. C_Z:
  775. result:=1;
  776. C_NC:
  777. result:=2;
  778. C_C:
  779. result:=3;
  780. C_PO:
  781. result:=4;
  782. C_PE:
  783. result:=5;
  784. C_P:
  785. result:=6;
  786. C_M:
  787. result:=7;
  788. else
  789. internalerror(2020050605);
  790. end;
  791. end;
  792. 'ppp':
  793. begin
  794. for i:=0 to insentry^.ops-1 do
  795. if insentry^.optypes[i]=OT_IMM_RST then
  796. begin
  797. if oper[i]^.typ<>top_const then
  798. internalerror(2020050606);
  799. result:=Byte(oper[i]^.val shr 3) and $07;
  800. end;
  801. end;
  802. 'bbb':
  803. begin
  804. for i:=0 to insentry^.ops-1 do
  805. if insentry^.optypes[i]=OT_IMM3 then
  806. begin
  807. if oper[i]^.typ<>top_const then
  808. internalerror(2020050606);
  809. result:=Byte(oper[i]^.val) and $07;
  810. end;
  811. end;
  812. 'bbbrrr':
  813. result:=(EvalMaskCode('bbb') shl 3) or EvalMaskCode('rrr');
  814. else
  815. internalerror(2020050409);
  816. end;
  817. end;
  818. procedure HandlePercent(token: string);
  819. var
  820. bincode: string;
  821. maskcode: string;
  822. i, valcode, shiftcount: integer;
  823. b: Byte;
  824. begin
  825. bincode:='';
  826. maskcode:='';
  827. for i:=1 to length(token) do
  828. case token[i] of
  829. '%':
  830. bincode:=bincode+'%';
  831. '0':
  832. begin
  833. bincode:=bincode+'0';
  834. maskcode:=maskcode+'0';
  835. end;
  836. '1':
  837. begin
  838. bincode:=bincode+'1';
  839. maskcode:=maskcode+'0';
  840. end;
  841. 'p','d','r','q','c','b':
  842. begin
  843. bincode:=bincode+'0';
  844. maskcode:=maskcode+token[i];
  845. end;
  846. '''':
  847. begin
  848. if (maskcode='') or (maskcode[length(maskcode)]<>'r') then
  849. internalerror(2020050408);
  850. maskcode[length(maskcode)]:='R';
  851. end;
  852. else
  853. internalerror(2020050405);
  854. end;
  855. Val(bincode,b,valcode);
  856. while maskcode[1]='0' do
  857. delete(maskcode,1,1);
  858. shiftcount:=0;
  859. while maskcode[length(maskcode)]='0' do
  860. begin
  861. delete(maskcode,length(maskcode),1);
  862. Inc(shiftcount);
  863. end;
  864. b:=b or (EvalMaskCode(maskcode) shl shiftcount);
  865. objdata.writebytes(b,1);
  866. end;
  867. var
  868. i: Integer;
  869. ch: Char;
  870. b: Byte;
  871. valcode: integer;
  872. code: string;
  873. token: string;
  874. begin
  875. { safety check }
  876. if objdata.currobjsec.size<>longword(insoffset) then
  877. internalerror(2020050401);
  878. code:=insentry^.code;
  879. i:=1;
  880. token:='';
  881. while i<=length(code) do
  882. begin
  883. ch:=code[i];
  884. Inc(i);
  885. if ch<>',' then
  886. token:=token+ch;
  887. if (ch=',') or (i>length(code)) then
  888. begin
  889. if token='' then
  890. internalerror(2020050402);
  891. if token[1]='$' then
  892. begin
  893. Val(token,b,valcode);
  894. WriteByte(b);
  895. end
  896. else if token[1]='%' then
  897. begin
  898. HandlePercent(token);
  899. end
  900. else if token='nn' then
  901. WriteNN
  902. else if token='n' then
  903. WriteN
  904. else if token='d' then
  905. WriteD
  906. else if token='e' then
  907. WriteE
  908. else
  909. internalerror(2020050503);
  910. token:='';
  911. end;
  912. end;
  913. end;
  914. procedure taicpu.init;
  915. begin
  916. insentry:=nil;
  917. LastInsOffset:=-1;
  918. InsOffset:=0;
  919. InsSize:=0;
  920. end;
  921. constructor taicpu.op_none(op : tasmop);
  922. begin
  923. inherited create(op);
  924. init;
  925. end;
  926. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  927. begin
  928. inherited create(op);
  929. init;
  930. ops:=1;
  931. loadreg(0,_op1);
  932. end;
  933. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  934. begin
  935. inherited create(op);
  936. init;
  937. ops:=1;
  938. loadref(0,_op1);
  939. end;
  940. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  941. begin
  942. inherited create(op);
  943. init;
  944. ops:=1;
  945. loadconst(0,_op1);
  946. end;
  947. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  948. begin
  949. inherited create(op);
  950. init;
  951. ops:=2;
  952. loadreg(0,_op1);
  953. loadreg(1,_op2);
  954. end;
  955. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  956. begin
  957. inherited create(op);
  958. init;
  959. ops:=2;
  960. loadreg(0,_op1);
  961. loadconst(1,_op2);
  962. end;
  963. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  964. begin
  965. inherited create(op);
  966. init;
  967. ops:=2;
  968. loadconst(0,_op1);
  969. loadreg(1,_op2);
  970. end;
  971. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  972. begin
  973. inherited create(op);
  974. init;
  975. ops:=2;
  976. loadreg(0,_op1);
  977. loadref(1,_op2);
  978. end;
  979. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  980. begin
  981. inherited create(op);
  982. init;
  983. ops:=2;
  984. loadref(0,_op1);
  985. loadreg(1,_op2);
  986. end;
  987. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  988. begin
  989. inherited create(op);
  990. init;
  991. ops:=2;
  992. loadref(0,_op1);
  993. loadconst(1,_op2);
  994. end;
  995. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  996. begin
  997. inherited create(op);
  998. init;
  999. is_jmp:=op in jmp_instructions;
  1000. condition:=cond;
  1001. ops:=1;
  1002. loadsymbol(0,_op1,0);
  1003. end;
  1004. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  1005. begin
  1006. inherited create(op);
  1007. init;
  1008. is_jmp:=op in jmp_instructions;
  1009. ops:=1;
  1010. loadsymbol(0,_op1,0);
  1011. end;
  1012. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  1013. begin
  1014. inherited create(op);
  1015. init;
  1016. ops:=1;
  1017. loadsymbol(0,_op1,_op1ofs);
  1018. end;
  1019. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1020. begin
  1021. result:=(
  1022. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  1023. ) and
  1024. (ops=2) and
  1025. (oper[0]^.typ=top_reg) and
  1026. (oper[1]^.typ=top_reg) and
  1027. (oper[0]^.reg=oper[1]^.reg);
  1028. end;
  1029. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1030. begin
  1031. result:=operand_read;
  1032. case opcode of
  1033. A_LD,
  1034. A_POP:
  1035. if opnr=0 then
  1036. result:=operand_write;
  1037. A_PUSH,
  1038. A_BIT,
  1039. A_CP,
  1040. A_DJNZ,
  1041. A_JR,
  1042. A_JP,
  1043. A_JRJP,
  1044. A_CALL,
  1045. A_RET,
  1046. A_RETI,
  1047. A_RETN,
  1048. A_RST,
  1049. A_IM:
  1050. ;
  1051. A_SET,
  1052. A_RES:
  1053. if opnr=1 then
  1054. result:=operand_readwrite;
  1055. A_EX:
  1056. result:=operand_readwrite;
  1057. else
  1058. begin
  1059. if opnr=0 then
  1060. result:=operand_readwrite;
  1061. end;
  1062. end;
  1063. end;
  1064. procedure taicpu.ResetPass1;
  1065. begin
  1066. { we need to reset everything here, because the choosen insentry
  1067. can be invalid for a new situation where the previously optimized
  1068. insentry is not correct }
  1069. InsEntry:=nil;
  1070. InsSize:=0;
  1071. LastInsOffset:=-1;
  1072. end;
  1073. procedure taicpu.ResetPass2;
  1074. begin
  1075. { we are here in a second pass, check if the instruction can be optimized }
  1076. if assigned(InsEntry) and (opcode=A_JRJP) then
  1077. begin
  1078. InsEntry:=nil;
  1079. InsSize:=0;
  1080. end;
  1081. LastInsOffset:=-1;
  1082. end;
  1083. function taicpu.Pass1(objdata: TObjData): longint;
  1084. begin
  1085. Pass1:=0;
  1086. { Save the old offset and set the new offset }
  1087. InsOffset:=ObjData.CurrObjSec.Size;
  1088. { Error? }
  1089. if (Insentry=nil) and (InsSize=-1) then
  1090. exit;
  1091. { set the file postion }
  1092. current_filepos:=fileinfo;
  1093. { Get InsEntry }
  1094. if FindInsEntry(ObjData) then
  1095. begin
  1096. { Calculate instruction size }
  1097. InsSize:=calcsize(insentry);
  1098. LastInsOffset:=InsOffset;
  1099. Pass1:=InsSize;
  1100. exit;
  1101. end;
  1102. LastInsOffset:=-1;
  1103. end;
  1104. procedure taicpu.Pass2(objdata: TObjData);
  1105. begin
  1106. { error in pass1 ? }
  1107. if insentry=nil then
  1108. exit;
  1109. current_filepos:=fileinfo;
  1110. { Generate the instruction }
  1111. GenCode(objdata);
  1112. end;
  1113. function taicpu.CheckIfValid: boolean;
  1114. begin
  1115. result:=FindInsEntry(nil);
  1116. end;
  1117. function taicpu.GetString: string;
  1118. var
  1119. i : longint;
  1120. s : string;
  1121. first: Boolean;
  1122. begin
  1123. s:='['+std_op2str[opcode];
  1124. for i:=0 to ops-1 do
  1125. begin
  1126. with oper[i]^ do
  1127. begin
  1128. if i=0 then
  1129. begin
  1130. s:=s+' ';
  1131. if condition<>C_None then
  1132. s:=s+cond2str[condition]+',';
  1133. end
  1134. else
  1135. s:=s+',';
  1136. case typ of
  1137. top_reg:
  1138. s:=s+std_regname(reg);
  1139. top_const:
  1140. s:=s+'const';
  1141. top_ref:
  1142. case ref^.refaddr of
  1143. addr_full:
  1144. s:=s+'addr16';
  1145. addr_lo8:
  1146. s:=s+'addr_lo8';
  1147. addr_hi8:
  1148. s:=s+'addr_hi8';
  1149. addr_no:
  1150. begin
  1151. s:=s+'(';
  1152. first:=true;
  1153. if ref^.base<>NR_NO then
  1154. begin
  1155. first:=false;
  1156. s:=s+std_regname(ref^.base);
  1157. end;
  1158. if ref^.index<>NR_NO then
  1159. begin
  1160. if not first then
  1161. s:=s+'+';
  1162. first:=false;
  1163. s:=s+std_regname(ref^.index);
  1164. end;
  1165. if assigned(ref^.symbol) then
  1166. begin
  1167. if not first then
  1168. s:=s+'+';
  1169. first:=false;
  1170. s:=s+'addr16';
  1171. end;
  1172. if ref^.offset<>0 then
  1173. begin
  1174. if not first then
  1175. s:=s+'+';
  1176. if (ref^.offset>=-128) and (ref^.offset<=127) then
  1177. s:=s+'const8'
  1178. else
  1179. s:=s+'const16';
  1180. end;
  1181. s:=s+')';
  1182. end;
  1183. else
  1184. ;
  1185. end;
  1186. else
  1187. ;
  1188. end;
  1189. end;
  1190. end;
  1191. GetString:=s+']';
  1192. end;
  1193. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1194. begin
  1195. case getregtype(r) of
  1196. R_INTREGISTER :
  1197. result:=taicpu.op_reg_ref(A_LD,r,ref)
  1198. else
  1199. internalerror(200401041);
  1200. end;
  1201. end;
  1202. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1203. begin
  1204. case getregtype(r) of
  1205. R_INTREGISTER :
  1206. result:=taicpu.op_ref_reg(A_LD,ref,r);
  1207. else
  1208. internalerror(200401041);
  1209. end;
  1210. end;
  1211. function is_ref_addr16(const ref: treference): Boolean;
  1212. begin
  1213. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  1214. end;
  1215. function is_ref_bc(const ref: treference): Boolean;
  1216. begin
  1217. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  1218. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  1219. (ref.offset=0) and (ref.scalefactor<=1) and
  1220. (ref.symbol=nil) and (ref.relsymbol=nil);
  1221. end;
  1222. function is_ref_de(const ref: treference): Boolean;
  1223. begin
  1224. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  1225. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  1226. (ref.offset=0) and (ref.scalefactor<=1) and
  1227. (ref.symbol=nil) and (ref.relsymbol=nil);
  1228. end;
  1229. function is_ref_hl(const ref: treference): Boolean;
  1230. begin
  1231. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  1232. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  1233. (ref.offset=0) and (ref.scalefactor<=1) and
  1234. (ref.symbol=nil) and (ref.relsymbol=nil);
  1235. end;
  1236. function is_ref_sp(const ref: treference): Boolean;
  1237. begin
  1238. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  1239. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  1240. (ref.offset=0) and (ref.scalefactor<=1) and
  1241. (ref.symbol=nil) and (ref.relsymbol=nil);
  1242. end;
  1243. function is_ref_ix(const ref: treference): Boolean;
  1244. begin
  1245. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1246. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1247. (ref.offset=0) and (ref.scalefactor<=1) and
  1248. (ref.symbol=nil) and (ref.relsymbol=nil);
  1249. end;
  1250. function is_ref_iy(const ref: treference): Boolean;
  1251. begin
  1252. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1253. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1254. (ref.offset=0) and (ref.scalefactor<=1) and
  1255. (ref.symbol=nil) and (ref.relsymbol=nil);
  1256. end;
  1257. function is_ref_ix_d(const ref: treference): Boolean;
  1258. begin
  1259. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1260. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1261. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1262. (ref.symbol=nil) and (ref.relsymbol=nil);
  1263. end;
  1264. function is_ref_iy_d(const ref: treference): Boolean;
  1265. begin
  1266. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1267. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1268. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1269. (ref.symbol=nil) and (ref.relsymbol=nil);
  1270. end;
  1271. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  1272. begin
  1273. case opertype of
  1274. OT_REF_ADDR16:
  1275. result:=is_ref_addr16(ref);
  1276. OT_REF_BC:
  1277. result:=is_ref_bc(ref);
  1278. OT_REF_DE:
  1279. result:=is_ref_de(ref);
  1280. OT_REF_HL:
  1281. result:=is_ref_hl(ref);
  1282. OT_REF_SP:
  1283. result:=is_ref_sp(ref);
  1284. OT_REF_IX:
  1285. result:=is_ref_ix(ref);
  1286. OT_REF_IY:
  1287. result:=is_ref_iy(ref);
  1288. OT_REF_IX_d:
  1289. result:=is_ref_ix_d(ref);
  1290. OT_REF_IY_d:
  1291. result:=is_ref_iy_d(ref);
  1292. else
  1293. internalerror(2020041801);
  1294. end;
  1295. end;
  1296. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  1297. var
  1298. ot: trefoperandtype;
  1299. begin
  1300. result:=true;
  1301. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  1302. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  1303. exit;
  1304. result:=false;
  1305. end;
  1306. {****************************************************************************
  1307. Instruction table
  1308. *****************************************************************************}
  1309. procedure BuildInsTabCache;
  1310. var
  1311. i : longint;
  1312. begin
  1313. new(instabcache);
  1314. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1315. i:=0;
  1316. while (i<InsTabEntries) do
  1317. begin
  1318. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1319. InsTabCache^[InsTab[i].OPcode]:=i;
  1320. inc(i);
  1321. end;
  1322. end;
  1323. procedure InitAsm;
  1324. begin
  1325. if not assigned(instabcache) then
  1326. BuildInsTabCache;
  1327. end;
  1328. procedure DoneAsm;
  1329. begin
  1330. if assigned(instabcache) then
  1331. begin
  1332. dispose(instabcache);
  1333. instabcache:=nil;
  1334. end;
  1335. end;
  1336. begin
  1337. cai_cpu:=taicpu;
  1338. cai_align:=tai_align;
  1339. end.