aasmcpu.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. i:=1;
  847. token:='';
  848. while i<=length(code) do
  849. begin
  850. ch:=code[i];
  851. Inc(i);
  852. if ch<>',' then
  853. token:=token+ch;
  854. if (ch=',') or (i>length(code)) then
  855. begin
  856. if token='' then
  857. internalerror(2020050402);
  858. if token[1]='$' then
  859. begin
  860. Val(token,b,valcode);
  861. WriteByte(b);
  862. end
  863. else if token[1]='%' then
  864. begin
  865. HandlePercent(token);
  866. end
  867. else if token='nn' then
  868. WriteNN
  869. else if token='n' then
  870. WriteN
  871. else if token='d' then
  872. WriteD
  873. else if token='e' then
  874. WriteE
  875. else
  876. internalerror(2020050503);
  877. token:='';
  878. end;
  879. end;
  880. end;
  881. constructor taicpu.op_none(op : tasmop);
  882. begin
  883. inherited create(op);
  884. end;
  885. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  886. begin
  887. inherited create(op);
  888. ops:=1;
  889. loadreg(0,_op1);
  890. end;
  891. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  892. begin
  893. inherited create(op);
  894. ops:=1;
  895. loadref(0,_op1);
  896. end;
  897. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  898. begin
  899. inherited create(op);
  900. ops:=1;
  901. loadconst(0,_op1);
  902. end;
  903. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  904. begin
  905. inherited create(op);
  906. ops:=2;
  907. loadreg(0,_op1);
  908. loadreg(1,_op2);
  909. end;
  910. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  911. begin
  912. inherited create(op);
  913. ops:=2;
  914. loadreg(0,_op1);
  915. loadconst(1,_op2);
  916. end;
  917. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  918. begin
  919. inherited create(op);
  920. ops:=2;
  921. loadconst(0,_op1);
  922. loadreg(1,_op2);
  923. end;
  924. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  925. begin
  926. inherited create(op);
  927. ops:=2;
  928. loadreg(0,_op1);
  929. loadref(1,_op2);
  930. end;
  931. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  932. begin
  933. inherited create(op);
  934. ops:=2;
  935. loadref(0,_op1);
  936. loadreg(1,_op2);
  937. end;
  938. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  939. begin
  940. inherited create(op);
  941. ops:=2;
  942. loadref(0,_op1);
  943. loadconst(1,_op2);
  944. end;
  945. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  946. begin
  947. inherited create(op);
  948. is_jmp:=op in jmp_instructions;
  949. condition:=cond;
  950. ops:=1;
  951. loadsymbol(0,_op1,0);
  952. end;
  953. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  954. begin
  955. inherited create(op);
  956. is_jmp:=op in jmp_instructions;
  957. ops:=1;
  958. loadsymbol(0,_op1,0);
  959. end;
  960. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  961. begin
  962. inherited create(op);
  963. ops:=1;
  964. loadsymbol(0,_op1,_op1ofs);
  965. end;
  966. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  967. begin
  968. result:=(
  969. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  970. ) and
  971. (ops=2) and
  972. (oper[0]^.typ=top_reg) and
  973. (oper[1]^.typ=top_reg) and
  974. (oper[0]^.reg=oper[1]^.reg);
  975. end;
  976. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  977. begin
  978. result:=operand_read;
  979. case opcode of
  980. A_LD,
  981. A_POP:
  982. if opnr=0 then
  983. result:=operand_write;
  984. A_PUSH,
  985. A_BIT,
  986. A_CP,
  987. A_DJNZ,
  988. A_JR,
  989. A_JP,
  990. A_CALL,
  991. A_RET,
  992. A_RETI,
  993. A_RETN,
  994. A_RST,
  995. A_IM:
  996. ;
  997. A_SET,
  998. A_RES:
  999. if opnr=1 then
  1000. result:=operand_readwrite;
  1001. A_EX:
  1002. result:=operand_readwrite;
  1003. else
  1004. begin
  1005. if opnr=0 then
  1006. result:=operand_readwrite;
  1007. end;
  1008. end;
  1009. end;
  1010. procedure taicpu.ResetPass1;
  1011. begin
  1012. { we need to reset everything here, because the choosen insentry
  1013. can be invalid for a new situation where the previously optimized
  1014. insentry is not correct }
  1015. InsEntry:=nil;
  1016. InsSize:=0;
  1017. LastInsOffset:=-1;
  1018. end;
  1019. procedure taicpu.ResetPass2;
  1020. begin
  1021. { we are here in a second pass, check if the instruction can be optimized }
  1022. {if assigned(InsEntry) and
  1023. (IF_PASS2 in InsEntry^.flags) then
  1024. begin
  1025. InsEntry:=nil;
  1026. InsSize:=0;
  1027. end;}
  1028. LastInsOffset:=-1;
  1029. end;
  1030. function taicpu.Pass1(objdata: TObjData): longint;
  1031. begin
  1032. Pass1:=0;
  1033. { Save the old offset and set the new offset }
  1034. InsOffset:=ObjData.CurrObjSec.Size;
  1035. { Error? }
  1036. if (Insentry=nil) and (InsSize=-1) then
  1037. exit;
  1038. { set the file postion }
  1039. current_filepos:=fileinfo;
  1040. { Get InsEntry }
  1041. if FindInsEntry(ObjData) then
  1042. begin
  1043. { Calculate instruction size }
  1044. InsSize:=calcsize(insentry);
  1045. LastInsOffset:=InsOffset;
  1046. Pass1:=InsSize;
  1047. exit;
  1048. end;
  1049. LastInsOffset:=-1;
  1050. end;
  1051. procedure taicpu.Pass2(objdata: TObjData);
  1052. begin
  1053. { error in pass1 ? }
  1054. if insentry=nil then
  1055. exit;
  1056. current_filepos:=fileinfo;
  1057. { Generate the instruction }
  1058. GenCode(objdata);
  1059. end;
  1060. function taicpu.CheckIfValid: boolean;
  1061. begin
  1062. result:=FindInsEntry(nil);
  1063. end;
  1064. function taicpu.GetString: string;
  1065. var
  1066. i : longint;
  1067. s : string;
  1068. first: Boolean;
  1069. begin
  1070. s:='['+std_op2str[opcode];
  1071. for i:=0 to ops-1 do
  1072. begin
  1073. with oper[i]^ do
  1074. begin
  1075. if i=0 then
  1076. begin
  1077. s:=s+' ';
  1078. if condition<>C_None then
  1079. s:=s+cond2str[condition]+',';
  1080. end
  1081. else
  1082. s:=s+',';
  1083. case typ of
  1084. top_reg:
  1085. s:=s+std_regname(reg);
  1086. top_const:
  1087. s:=s+'const';
  1088. top_ref:
  1089. case ref^.refaddr of
  1090. addr_full:
  1091. s:=s+'addr16';
  1092. addr_lo8:
  1093. s:=s+'addr_lo8';
  1094. addr_hi8:
  1095. s:=s+'addr_hi8';
  1096. addr_no:
  1097. begin
  1098. s:=s+'(';
  1099. first:=true;
  1100. if ref^.base<>NR_NO then
  1101. begin
  1102. first:=false;
  1103. s:=s+std_regname(ref^.base);
  1104. end;
  1105. if ref^.index<>NR_NO then
  1106. begin
  1107. if not first then
  1108. s:=s+'+';
  1109. first:=false;
  1110. s:=s+std_regname(ref^.index);
  1111. end;
  1112. if assigned(ref^.symbol) then
  1113. begin
  1114. if not first then
  1115. s:=s+'+';
  1116. first:=false;
  1117. s:=s+'addr16';
  1118. end;
  1119. if ref^.offset<>0 then
  1120. begin
  1121. if not first then
  1122. s:=s+'+';
  1123. if (ref^.offset>=-128) and (ref^.offset<=127) then
  1124. s:=s+'const8'
  1125. else
  1126. s:=s+'const16';
  1127. end;
  1128. s:=s+')';
  1129. end;
  1130. else
  1131. ;
  1132. end;
  1133. else
  1134. ;
  1135. end;
  1136. end;
  1137. end;
  1138. GetString:=s+']';
  1139. end;
  1140. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1141. begin
  1142. case getregtype(r) of
  1143. R_INTREGISTER :
  1144. result:=taicpu.op_reg_ref(A_LD,r,ref)
  1145. else
  1146. internalerror(200401041);
  1147. end;
  1148. end;
  1149. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1150. begin
  1151. case getregtype(r) of
  1152. R_INTREGISTER :
  1153. result:=taicpu.op_ref_reg(A_LD,ref,r);
  1154. else
  1155. internalerror(200401041);
  1156. end;
  1157. end;
  1158. function is_ref_addr16(const ref: treference): Boolean;
  1159. begin
  1160. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  1161. end;
  1162. function is_ref_bc(const ref: treference): Boolean;
  1163. begin
  1164. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  1165. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  1166. (ref.offset=0) and (ref.scalefactor<=1) and
  1167. (ref.symbol=nil) and (ref.relsymbol=nil);
  1168. end;
  1169. function is_ref_de(const ref: treference): Boolean;
  1170. begin
  1171. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  1172. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  1173. (ref.offset=0) and (ref.scalefactor<=1) and
  1174. (ref.symbol=nil) and (ref.relsymbol=nil);
  1175. end;
  1176. function is_ref_hl(const ref: treference): Boolean;
  1177. begin
  1178. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  1179. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  1180. (ref.offset=0) and (ref.scalefactor<=1) and
  1181. (ref.symbol=nil) and (ref.relsymbol=nil);
  1182. end;
  1183. function is_ref_sp(const ref: treference): Boolean;
  1184. begin
  1185. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  1186. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  1187. (ref.offset=0) and (ref.scalefactor<=1) and
  1188. (ref.symbol=nil) and (ref.relsymbol=nil);
  1189. end;
  1190. function is_ref_ix(const ref: treference): Boolean;
  1191. begin
  1192. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1193. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1194. (ref.offset=0) and (ref.scalefactor<=1) and
  1195. (ref.symbol=nil) and (ref.relsymbol=nil);
  1196. end;
  1197. function is_ref_iy(const ref: treference): Boolean;
  1198. begin
  1199. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1200. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1201. (ref.offset=0) and (ref.scalefactor<=1) and
  1202. (ref.symbol=nil) and (ref.relsymbol=nil);
  1203. end;
  1204. function is_ref_ix_d(const ref: treference): Boolean;
  1205. begin
  1206. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1207. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1208. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1209. (ref.symbol=nil) and (ref.relsymbol=nil);
  1210. end;
  1211. function is_ref_iy_d(const ref: treference): Boolean;
  1212. begin
  1213. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1214. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1215. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1216. (ref.symbol=nil) and (ref.relsymbol=nil);
  1217. end;
  1218. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  1219. begin
  1220. case opertype of
  1221. OT_REF_ADDR16:
  1222. result:=is_ref_addr16(ref);
  1223. OT_REF_BC:
  1224. result:=is_ref_bc(ref);
  1225. OT_REF_DE:
  1226. result:=is_ref_de(ref);
  1227. OT_REF_HL:
  1228. result:=is_ref_hl(ref);
  1229. OT_REF_SP:
  1230. result:=is_ref_sp(ref);
  1231. OT_REF_IX:
  1232. result:=is_ref_ix(ref);
  1233. OT_REF_IY:
  1234. result:=is_ref_iy(ref);
  1235. OT_REF_IX_d:
  1236. result:=is_ref_ix_d(ref);
  1237. OT_REF_IY_d:
  1238. result:=is_ref_iy_d(ref);
  1239. else
  1240. internalerror(2020041801);
  1241. end;
  1242. end;
  1243. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  1244. var
  1245. ot: trefoperandtype;
  1246. begin
  1247. result:=true;
  1248. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  1249. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  1250. exit;
  1251. result:=false;
  1252. end;
  1253. {****************************************************************************
  1254. Instruction table
  1255. *****************************************************************************}
  1256. procedure BuildInsTabCache;
  1257. var
  1258. i : longint;
  1259. begin
  1260. new(instabcache);
  1261. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1262. i:=0;
  1263. while (i<InsTabEntries) do
  1264. begin
  1265. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1266. InsTabCache^[InsTab[i].OPcode]:=i;
  1267. inc(i);
  1268. end;
  1269. end;
  1270. procedure InitAsm;
  1271. begin
  1272. if not assigned(instabcache) then
  1273. BuildInsTabCache;
  1274. end;
  1275. procedure DoneAsm;
  1276. begin
  1277. if assigned(instabcache) then
  1278. begin
  1279. dispose(instabcache);
  1280. instabcache:=nil;
  1281. end;
  1282. end;
  1283. begin
  1284. cai_cpu:=taicpu;
  1285. cai_align:=tai_align;
  1286. end.