aasmcpu.pas 46 KB

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