aasmcpu.pas 48 KB

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