aasmcpu.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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. else
  472. InternalError(2020050613);
  473. end;
  474. InternalError(2020050403);
  475. end;
  476. procedure WriteN;
  477. var
  478. i: Integer;
  479. begin
  480. for i:=0 to insentry^.ops-1 do
  481. begin
  482. if insentry^.optypes[i]=OT_IMM8 then
  483. begin
  484. case oper[i]^.typ of
  485. top_const:
  486. begin
  487. WriteByte(Byte(oper[i]^.val));
  488. exit;
  489. end;
  490. top_ref:
  491. begin
  492. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  493. internalerror(2020050507);
  494. if Assigned(oper[i]^.ref^.symbol) then
  495. begin
  496. case oper[i]^.ref^.refaddr of
  497. addr_hi8:
  498. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_HI8);
  499. addr_lo8:
  500. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_LO8);
  501. else
  502. internalerror(2020050408);
  503. end;
  504. exit;
  505. end
  506. else
  507. internalerror(2020050409);
  508. end;
  509. else
  510. InternalError(2020050506);
  511. end;
  512. end
  513. else if insentry^.optypes[i]=OT_IMM_PORT then
  514. begin
  515. case oper[i]^.typ of
  516. top_ref:
  517. begin
  518. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) or Assigned(oper[i]^.ref^.symbol) then
  519. internalerror(2020050612);
  520. WriteByte(Byte(oper[i]^.ref^.offset));
  521. exit;
  522. end;
  523. else
  524. InternalError(2020050611);
  525. end;
  526. end;
  527. end;
  528. InternalError(2020050505);
  529. end;
  530. procedure WriteD;
  531. var
  532. i: Integer;
  533. begin
  534. for i:=0 to insentry^.ops-1 do
  535. begin
  536. if insentry^.optypes[i] in [OT_REF_IX_d,OT_REF_IY_d] then
  537. begin
  538. case oper[i]^.typ of
  539. top_ref:
  540. begin
  541. if not is_ref_opertype(oper[i]^.ref^,insentry^.optypes[i]) then
  542. internalerror(2020050510);
  543. WriteByte(Byte(oper[i]^.ref^.offset));
  544. exit;
  545. end;
  546. else
  547. InternalError(2020050511);
  548. end;
  549. end;
  550. end;
  551. InternalError(2020050512);
  552. end;
  553. procedure WriteE;
  554. var
  555. i, j: Integer;
  556. begin
  557. for j:=0 to insentry^.ops-1 do
  558. begin
  559. if condition=C_NONE then
  560. i:=j
  561. else
  562. i:=j-1;
  563. if insentry^.optypes[j]=OT_RELJMP8 then
  564. begin
  565. case oper[i]^.typ of
  566. top_ref:
  567. begin
  568. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  569. internalerror(2020050608);
  570. if Assigned(oper[i]^.ref^.symbol) then
  571. begin
  572. if oper[i]^.ref^.refaddr<>addr_full then
  573. internalerror(2020050609);
  574. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_RELATIVE);
  575. exit;
  576. end
  577. else
  578. internalerror(2020050610);
  579. exit;
  580. end;
  581. else
  582. InternalError(2020050607);
  583. end;
  584. end;
  585. end;
  586. end;
  587. function EvalMaskCode(const maskcode: string): byte;
  588. var
  589. i: Integer;
  590. begin
  591. case maskcode of
  592. 'dd':
  593. for i:=0 to insentry^.ops-1 do
  594. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_SP then
  595. begin
  596. if oper[i]^.typ<>top_reg then
  597. internalerror(2020050410);
  598. case oper[i]^.reg of
  599. NR_BC:
  600. result:=0;
  601. NR_DE:
  602. result:=1;
  603. NR_HL:
  604. result:=2;
  605. NR_SP:
  606. result:=3;
  607. else
  608. internalerror(2020050411);
  609. end;
  610. end;
  611. 'qq':
  612. for i:=0 to insentry^.ops-1 do
  613. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_AF then
  614. begin
  615. if oper[i]^.typ<>top_reg then
  616. internalerror(2020050412);
  617. case oper[i]^.reg of
  618. NR_BC:
  619. result:=0;
  620. NR_DE:
  621. result:=1;
  622. NR_HL:
  623. result:=2;
  624. NR_AF:
  625. result:=3;
  626. else
  627. internalerror(2020050413);
  628. end;
  629. end;
  630. 'pp':
  631. for i:=0 to insentry^.ops-1 do
  632. if insentry^.optypes[i]=OT_REG16_BC_DE_IX_SP then
  633. begin
  634. if oper[i]^.typ<>top_reg then
  635. internalerror(2020050414);
  636. case oper[i]^.reg of
  637. NR_BC:
  638. result:=0;
  639. NR_DE:
  640. result:=1;
  641. NR_IX:
  642. result:=2;
  643. NR_SP:
  644. result:=3;
  645. else
  646. internalerror(2020050415);
  647. end;
  648. end;
  649. 'rr':
  650. for i:=0 to insentry^.ops-1 do
  651. if insentry^.optypes[i]=OT_REG16_BC_DE_IY_SP then
  652. begin
  653. if oper[i]^.typ<>top_reg then
  654. internalerror(2020050416);
  655. case oper[i]^.reg of
  656. NR_BC:
  657. result:=0;
  658. NR_DE:
  659. result:=1;
  660. NR_IY:
  661. result:=2;
  662. NR_SP:
  663. result:=3;
  664. else
  665. internalerror(2020050417);
  666. end;
  667. end;
  668. 'rrr':
  669. for i:=0 to insentry^.ops-1 do
  670. if insentry^.optypes[i]=OT_REG8 then
  671. begin
  672. if oper[i]^.typ<>top_reg then
  673. internalerror(2020050418);
  674. case oper[i]^.reg of
  675. NR_A:
  676. result:=7;
  677. NR_B:
  678. result:=0;
  679. NR_C:
  680. result:=1;
  681. NR_D:
  682. result:=2;
  683. NR_E:
  684. result:=3;
  685. NR_H:
  686. result:=4;
  687. NR_L:
  688. result:=5;
  689. else
  690. internalerror(2020050419);
  691. end;
  692. end;
  693. 'rrrRRR':
  694. begin
  695. if ops<>2 then
  696. internalerror(2020050420);
  697. if (insentry^.optypes[0]<>OT_REG8) or (insentry^.optypes[1]<>OT_REG8) then
  698. internalerror(2020050421);
  699. if (oper[0]^.typ<>top_reg) or (oper[1]^.typ<>top_reg) then
  700. internalerror(2020050422);
  701. case oper[0]^.reg of
  702. NR_A:
  703. result:=7 shl 3;
  704. NR_B:
  705. result:=0 shl 3;
  706. NR_C:
  707. result:=1 shl 3;
  708. NR_D:
  709. result:=2 shl 3;
  710. NR_E:
  711. result:=3 shl 3;
  712. NR_H:
  713. result:=4 shl 3;
  714. NR_L:
  715. result:=5 shl 3;
  716. else
  717. internalerror(2020050419);
  718. end;
  719. case oper[1]^.reg of
  720. NR_A:
  721. result:=result or 7;
  722. NR_B:
  723. result:=result or 0;
  724. NR_C:
  725. result:=result or 1;
  726. NR_D:
  727. result:=result or 2;
  728. NR_E:
  729. result:=result or 3;
  730. NR_H:
  731. result:=result or 4;
  732. NR_L:
  733. result:=result or 5;
  734. else
  735. internalerror(2020050419);
  736. end;
  737. end;
  738. 'ccc':
  739. begin
  740. case condition of
  741. C_NZ:
  742. result:=0;
  743. C_Z:
  744. result:=1;
  745. C_NC:
  746. result:=2;
  747. C_C:
  748. result:=3;
  749. C_PO:
  750. result:=4;
  751. C_PE:
  752. result:=5;
  753. C_P:
  754. result:=6;
  755. C_M:
  756. result:=7;
  757. else
  758. internalerror(2020050605);
  759. end;
  760. end;
  761. 'ppp':
  762. begin
  763. for i:=0 to insentry^.ops-1 do
  764. if insentry^.optypes[i]=OT_IMM_RST then
  765. begin
  766. if oper[i]^.typ<>top_const then
  767. internalerror(2020050606);
  768. result:=Byte(oper[i]^.val shr 3) and $07;
  769. end;
  770. end;
  771. 'bbb':
  772. begin
  773. for i:=0 to insentry^.ops-1 do
  774. if insentry^.optypes[i]=OT_IMM3 then
  775. begin
  776. if oper[i]^.typ<>top_const then
  777. internalerror(2020050606);
  778. result:=Byte(oper[i]^.val) and $07;
  779. end;
  780. end;
  781. 'bbbrrr':
  782. result:=(EvalMaskCode('bbb') shl 3) or EvalMaskCode('rrr');
  783. else
  784. internalerror(2020050409);
  785. end;
  786. end;
  787. procedure HandlePercent(token: string);
  788. var
  789. bincode: string;
  790. maskcode: string;
  791. i, valcode, shiftcount: integer;
  792. b: Byte;
  793. begin
  794. bincode:='';
  795. maskcode:='';
  796. for i:=1 to length(token) do
  797. case token[i] of
  798. '%':
  799. bincode:=bincode+'%';
  800. '0':
  801. begin
  802. bincode:=bincode+'0';
  803. maskcode:=maskcode+'0';
  804. end;
  805. '1':
  806. begin
  807. bincode:=bincode+'1';
  808. maskcode:=maskcode+'0';
  809. end;
  810. 'p','d','r','q','c','b':
  811. begin
  812. bincode:=bincode+'0';
  813. maskcode:=maskcode+token[i];
  814. end;
  815. '''':
  816. begin
  817. if (maskcode='') or (maskcode[length(maskcode)]<>'r') then
  818. internalerror(2020050408);
  819. maskcode[length(maskcode)]:='R';
  820. end;
  821. else
  822. internalerror(2020050405);
  823. end;
  824. Val(bincode,b,valcode);
  825. while maskcode[1]='0' do
  826. delete(maskcode,1,1);
  827. shiftcount:=0;
  828. while maskcode[length(maskcode)]='0' do
  829. begin
  830. delete(maskcode,length(maskcode),1);
  831. Inc(shiftcount);
  832. end;
  833. b:=b or (EvalMaskCode(maskcode) shl shiftcount);
  834. objdata.writebytes(b,1);
  835. end;
  836. var
  837. i: Integer;
  838. ch: Char;
  839. b: Byte;
  840. valcode: integer;
  841. code: string;
  842. token: string;
  843. begin
  844. { safety check }
  845. if objdata.currobjsec.size<>longword(insoffset) then
  846. internalerror(2020050401);
  847. code:=insentry^.code;
  848. //Writeln('>',code,'<');
  849. i:=1;
  850. token:='';
  851. while i<=length(code) do
  852. begin
  853. ch:=code[i];
  854. Inc(i);
  855. if ch<>',' then
  856. token:=token+ch;
  857. if (ch=',') or (i>length(code)) then
  858. begin
  859. if token='' then
  860. internalerror(2020050402);
  861. if token[1]='$' then
  862. begin
  863. Val(token,b,valcode);
  864. WriteByte(b);
  865. end
  866. else if token[1]='%' then
  867. begin
  868. HandlePercent(token);
  869. end
  870. else if token='nn' then
  871. WriteNN
  872. else if token='n' then
  873. WriteN
  874. else if token='d' then
  875. WriteD
  876. else if token='e' then
  877. WriteE
  878. else
  879. internalerror(2020050503);
  880. token:='';
  881. end;
  882. end;
  883. end;
  884. constructor taicpu.op_none(op : tasmop);
  885. begin
  886. inherited create(op);
  887. end;
  888. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  889. begin
  890. inherited create(op);
  891. ops:=1;
  892. loadreg(0,_op1);
  893. end;
  894. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  895. begin
  896. inherited create(op);
  897. ops:=1;
  898. loadref(0,_op1);
  899. end;
  900. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  901. begin
  902. inherited create(op);
  903. ops:=1;
  904. loadconst(0,_op1);
  905. end;
  906. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  907. begin
  908. inherited create(op);
  909. ops:=2;
  910. loadreg(0,_op1);
  911. loadreg(1,_op2);
  912. end;
  913. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  914. begin
  915. inherited create(op);
  916. ops:=2;
  917. loadreg(0,_op1);
  918. loadconst(1,_op2);
  919. end;
  920. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  921. begin
  922. inherited create(op);
  923. ops:=2;
  924. loadconst(0,_op1);
  925. loadreg(1,_op2);
  926. end;
  927. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  928. begin
  929. inherited create(op);
  930. ops:=2;
  931. loadreg(0,_op1);
  932. loadref(1,_op2);
  933. end;
  934. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  935. begin
  936. inherited create(op);
  937. ops:=2;
  938. loadref(0,_op1);
  939. loadreg(1,_op2);
  940. end;
  941. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  942. begin
  943. inherited create(op);
  944. ops:=2;
  945. loadref(0,_op1);
  946. loadconst(1,_op2);
  947. end;
  948. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  949. begin
  950. inherited create(op);
  951. is_jmp:=op in jmp_instructions;
  952. condition:=cond;
  953. ops:=1;
  954. loadsymbol(0,_op1,0);
  955. end;
  956. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  957. begin
  958. inherited create(op);
  959. is_jmp:=op in jmp_instructions;
  960. ops:=1;
  961. loadsymbol(0,_op1,0);
  962. end;
  963. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  964. begin
  965. inherited create(op);
  966. ops:=1;
  967. loadsymbol(0,_op1,_op1ofs);
  968. end;
  969. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  970. begin
  971. result:=(
  972. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  973. ) and
  974. (ops=2) and
  975. (oper[0]^.typ=top_reg) and
  976. (oper[1]^.typ=top_reg) and
  977. (oper[0]^.reg=oper[1]^.reg);
  978. end;
  979. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  980. begin
  981. result:=operand_read;
  982. case opcode of
  983. A_LD,
  984. A_POP:
  985. if opnr=0 then
  986. result:=operand_write;
  987. A_PUSH,
  988. A_BIT,
  989. A_CP,
  990. A_DJNZ,
  991. A_JR,
  992. A_JP,
  993. A_CALL,
  994. A_RET,
  995. A_RETI,
  996. A_RETN,
  997. A_RST,
  998. A_IM:
  999. ;
  1000. A_SET,
  1001. A_RES:
  1002. if opnr=1 then
  1003. result:=operand_readwrite;
  1004. A_EX:
  1005. result:=operand_readwrite;
  1006. else
  1007. begin
  1008. if opnr=0 then
  1009. result:=operand_readwrite;
  1010. end;
  1011. end;
  1012. end;
  1013. procedure taicpu.ResetPass1;
  1014. begin
  1015. { we need to reset everything here, because the choosen insentry
  1016. can be invalid for a new situation where the previously optimized
  1017. insentry is not correct }
  1018. InsEntry:=nil;
  1019. InsSize:=0;
  1020. LastInsOffset:=-1;
  1021. end;
  1022. procedure taicpu.ResetPass2;
  1023. begin
  1024. { we are here in a second pass, check if the instruction can be optimized }
  1025. {if assigned(InsEntry) and
  1026. (IF_PASS2 in InsEntry^.flags) then
  1027. begin
  1028. InsEntry:=nil;
  1029. InsSize:=0;
  1030. end;}
  1031. LastInsOffset:=-1;
  1032. end;
  1033. function taicpu.Pass1(objdata: TObjData): longint;
  1034. begin
  1035. Pass1:=0;
  1036. { Save the old offset and set the new offset }
  1037. InsOffset:=ObjData.CurrObjSec.Size;
  1038. { Error? }
  1039. if (Insentry=nil) and (InsSize=-1) then
  1040. exit;
  1041. { set the file postion }
  1042. current_filepos:=fileinfo;
  1043. { Get InsEntry }
  1044. if FindInsEntry(ObjData) then
  1045. begin
  1046. { Calculate instruction size }
  1047. InsSize:=calcsize(insentry);
  1048. LastInsOffset:=InsOffset;
  1049. Pass1:=InsSize;
  1050. exit;
  1051. end;
  1052. LastInsOffset:=-1;
  1053. end;
  1054. procedure taicpu.Pass2(objdata: TObjData);
  1055. begin
  1056. { error in pass1 ? }
  1057. if insentry=nil then
  1058. exit;
  1059. current_filepos:=fileinfo;
  1060. { Generate the instruction }
  1061. GenCode(objdata);
  1062. end;
  1063. function taicpu.CheckIfValid: boolean;
  1064. begin
  1065. result:=FindInsEntry(nil);
  1066. end;
  1067. function taicpu.GetString: string;
  1068. var
  1069. i : longint;
  1070. s : string;
  1071. first: Boolean;
  1072. begin
  1073. s:='['+std_op2str[opcode];
  1074. for i:=0 to ops-1 do
  1075. begin
  1076. with oper[i]^ do
  1077. begin
  1078. if i=0 then
  1079. begin
  1080. s:=s+' ';
  1081. if condition<>C_None then
  1082. s:=s+cond2str[condition]+',';
  1083. end
  1084. else
  1085. s:=s+',';
  1086. case typ of
  1087. top_reg:
  1088. s:=s+std_regname(reg);
  1089. top_const:
  1090. s:=s+'const';
  1091. top_ref:
  1092. case ref^.refaddr of
  1093. addr_full:
  1094. s:=s+'addr16';
  1095. addr_lo8:
  1096. s:=s+'addr_lo8';
  1097. addr_hi8:
  1098. s:=s+'addr_hi8';
  1099. addr_no:
  1100. begin
  1101. s:=s+'(';
  1102. first:=true;
  1103. if ref^.base<>NR_NO then
  1104. begin
  1105. first:=false;
  1106. s:=s+std_regname(ref^.base);
  1107. end;
  1108. if ref^.index<>NR_NO then
  1109. begin
  1110. if not first then
  1111. s:=s+'+';
  1112. first:=false;
  1113. s:=s+std_regname(ref^.index);
  1114. end;
  1115. if assigned(ref^.symbol) then
  1116. begin
  1117. if not first then
  1118. s:=s+'+';
  1119. first:=false;
  1120. s:=s+'addr16';
  1121. end;
  1122. if ref^.offset<>0 then
  1123. begin
  1124. if not first then
  1125. s:=s+'+';
  1126. if (ref^.offset>=-128) and (ref^.offset<=127) then
  1127. s:=s+'const8'
  1128. else
  1129. s:=s+'const16';
  1130. end;
  1131. s:=s+')';
  1132. end;
  1133. else
  1134. ;
  1135. end;
  1136. else
  1137. ;
  1138. end;
  1139. end;
  1140. end;
  1141. GetString:=s+']';
  1142. end;
  1143. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1144. begin
  1145. case getregtype(r) of
  1146. R_INTREGISTER :
  1147. result:=taicpu.op_reg_ref(A_LD,r,ref)
  1148. else
  1149. internalerror(200401041);
  1150. end;
  1151. end;
  1152. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1153. begin
  1154. case getregtype(r) of
  1155. R_INTREGISTER :
  1156. result:=taicpu.op_ref_reg(A_LD,ref,r);
  1157. else
  1158. internalerror(200401041);
  1159. end;
  1160. end;
  1161. function is_ref_addr16(const ref: treference): Boolean;
  1162. begin
  1163. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  1164. end;
  1165. function is_ref_bc(const ref: treference): Boolean;
  1166. begin
  1167. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  1168. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  1169. (ref.offset=0) and (ref.scalefactor<=1) and
  1170. (ref.symbol=nil) and (ref.relsymbol=nil);
  1171. end;
  1172. function is_ref_de(const ref: treference): Boolean;
  1173. begin
  1174. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  1175. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  1176. (ref.offset=0) and (ref.scalefactor<=1) and
  1177. (ref.symbol=nil) and (ref.relsymbol=nil);
  1178. end;
  1179. function is_ref_hl(const ref: treference): Boolean;
  1180. begin
  1181. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  1182. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  1183. (ref.offset=0) and (ref.scalefactor<=1) and
  1184. (ref.symbol=nil) and (ref.relsymbol=nil);
  1185. end;
  1186. function is_ref_sp(const ref: treference): Boolean;
  1187. begin
  1188. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  1189. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  1190. (ref.offset=0) and (ref.scalefactor<=1) and
  1191. (ref.symbol=nil) and (ref.relsymbol=nil);
  1192. end;
  1193. function is_ref_ix(const ref: treference): Boolean;
  1194. begin
  1195. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1196. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1197. (ref.offset=0) and (ref.scalefactor<=1) and
  1198. (ref.symbol=nil) and (ref.relsymbol=nil);
  1199. end;
  1200. function is_ref_iy(const ref: treference): Boolean;
  1201. begin
  1202. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1203. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1204. (ref.offset=0) and (ref.scalefactor<=1) and
  1205. (ref.symbol=nil) and (ref.relsymbol=nil);
  1206. end;
  1207. function is_ref_ix_d(const ref: treference): Boolean;
  1208. begin
  1209. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1210. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1211. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1212. (ref.symbol=nil) and (ref.relsymbol=nil);
  1213. end;
  1214. function is_ref_iy_d(const ref: treference): Boolean;
  1215. begin
  1216. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1217. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1218. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1219. (ref.symbol=nil) and (ref.relsymbol=nil);
  1220. end;
  1221. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  1222. begin
  1223. case opertype of
  1224. OT_REF_ADDR16:
  1225. result:=is_ref_addr16(ref);
  1226. OT_REF_BC:
  1227. result:=is_ref_bc(ref);
  1228. OT_REF_DE:
  1229. result:=is_ref_de(ref);
  1230. OT_REF_HL:
  1231. result:=is_ref_hl(ref);
  1232. OT_REF_SP:
  1233. result:=is_ref_sp(ref);
  1234. OT_REF_IX:
  1235. result:=is_ref_ix(ref);
  1236. OT_REF_IY:
  1237. result:=is_ref_iy(ref);
  1238. OT_REF_IX_d:
  1239. result:=is_ref_ix_d(ref);
  1240. OT_REF_IY_d:
  1241. result:=is_ref_iy_d(ref);
  1242. else
  1243. internalerror(2020041801);
  1244. end;
  1245. end;
  1246. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  1247. var
  1248. ot: trefoperandtype;
  1249. begin
  1250. result:=true;
  1251. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  1252. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  1253. exit;
  1254. result:=false;
  1255. end;
  1256. {****************************************************************************
  1257. Instruction table
  1258. *****************************************************************************}
  1259. procedure BuildInsTabCache;
  1260. var
  1261. i : longint;
  1262. begin
  1263. new(instabcache);
  1264. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1265. i:=0;
  1266. while (i<InsTabEntries) do
  1267. begin
  1268. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1269. InsTabCache^[InsTab[i].OPcode]:=i;
  1270. inc(i);
  1271. end;
  1272. end;
  1273. procedure InitAsm;
  1274. begin
  1275. if not assigned(instabcache) then
  1276. BuildInsTabCache;
  1277. end;
  1278. procedure DoneAsm;
  1279. begin
  1280. if assigned(instabcache) then
  1281. begin
  1282. dispose(instabcache);
  1283. instabcache:=nil;
  1284. end;
  1285. end;
  1286. begin
  1287. cai_cpu:=taicpu;
  1288. cai_align:=tai_align;
  1289. end.