aasmcpu.pas 44 KB

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