cgcpu.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. {
  2. Copyright (c) 2008 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the Z80
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,node,cg64f32,rgcpu;
  27. type
  28. tcgz80 = class(tcg)
  29. { true, if the next arithmetic operation should modify the flags }
  30. cgsetflags : boolean;
  31. procedure init_register_allocators;override;
  32. procedure done_register_allocators;override;
  33. function getaddressregister(list:TAsmList):TRegister;override;
  34. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  35. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  36. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  37. procedure a_load_reg_cgpara(list : TAsmList; size : tcgsize;r : tregister; const cgpara : tcgpara);override;
  38. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  39. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  40. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  41. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src, dst : TRegister); override;
  42. { move instructions }
  43. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  44. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  45. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  46. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  47. { fpu move instructions }
  48. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  49. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  50. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  51. { comparison operations }
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  53. l : tasmlabel);override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  59. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  60. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  61. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  62. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  63. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  64. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  65. procedure g_save_registers(list : TAsmList);override;
  66. procedure g_restore_registers(list : TAsmList);override;
  67. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  68. procedure fixref(list : TAsmList;var ref : treference);
  69. function normalize_ref(list : TAsmList;ref : treference;
  70. tmpreg : tregister) : treference;
  71. procedure emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  72. procedure a_adjust_sp(list: TAsmList; value: longint);
  73. protected
  74. procedure a_op_reg_reg_internal(list: TAsmList; Op: TOpCG; size: TCGSize; src, srchi, dst, dsthi: TRegister);
  75. procedure a_op_const_reg_internal(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg, reghi: TRegister);
  76. procedure maybegetcpuregister(list : tasmlist; reg : tregister);
  77. end;
  78. tcg64fz80 = class(tcg64f32)
  79. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  80. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  81. end;
  82. function GetByteLoc(const loc : tlocation;nr : byte) : tlocation;
  83. procedure create_codegen;
  84. const
  85. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_LD,A_ADD,A_AND,A_NONE,
  86. A_NONE,A_NONE,A_NONE,A_NEG,A_CPL,A_OR,
  87. A_SRA,A_SLA,A_SRL,A_SUB,A_XOR,A_RLCA,A_RRCA);
  88. implementation
  89. uses
  90. globals,verbose,systems,cutils,
  91. fmodule,
  92. symconst,symsym,symtable,
  93. tgobj,rgobj,
  94. procinfo,cpupi,
  95. paramgr;
  96. procedure tcgz80.init_register_allocators;
  97. begin
  98. inherited init_register_allocators;
  99. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  100. [RS_BC,RS_DE,RS_HL],first_int_imreg,[]);
  101. end;
  102. procedure tcgz80.done_register_allocators;
  103. begin
  104. rg[R_INTREGISTER].free;
  105. // rg[R_ADDRESSREGISTER].free;
  106. inherited done_register_allocators;
  107. end;
  108. function tcgz80.getaddressregister(list: TAsmList): TRegister;
  109. begin
  110. Result:=getintregister(list,OS_ADDR);
  111. end;
  112. procedure tcgz80.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  113. procedure load_para_loc(r : TRegister;paraloc : PCGParaLocation);
  114. var
  115. ref : treference;
  116. begin
  117. paramanager.allocparaloc(list,paraloc);
  118. case paraloc^.loc of
  119. LOC_REGISTER,LOC_CREGISTER:
  120. a_load_reg_reg(list,paraloc^.size,paraloc^.size,r,paraloc^.register);
  121. LOC_REFERENCE,LOC_CREFERENCE:
  122. begin
  123. reference_reset_base(ref,paraloc^.reference.index,paraloc^.reference.offset,ctempposinvalid,2,[]);
  124. a_load_reg_ref(list,paraloc^.size,paraloc^.size,r,ref);
  125. end;
  126. else
  127. internalerror(2002071004);
  128. end;
  129. end;
  130. var
  131. i, i2 : longint;
  132. hp : PCGParaLocation;
  133. begin
  134. { if use_push(cgpara) then
  135. begin
  136. if tcgsize2size[cgpara.Size] > 2 then
  137. begin
  138. if tcgsize2size[cgpara.Size] <> 4 then
  139. internalerror(2013031101);
  140. if cgpara.location^.Next = nil then
  141. begin
  142. if tcgsize2size[cgpara.location^.size] <> 4 then
  143. internalerror(2013031101);
  144. end
  145. else
  146. begin
  147. if tcgsize2size[cgpara.location^.size] <> 2 then
  148. internalerror(2013031101);
  149. if tcgsize2size[cgpara.location^.Next^.size] <> 2 then
  150. internalerror(2013031101);
  151. if cgpara.location^.Next^.Next <> nil then
  152. internalerror(2013031101);
  153. end;
  154. if tcgsize2size[cgpara.size]>cgpara.alignment then
  155. pushsize:=cgpara.size
  156. else
  157. pushsize:=int_cgsize(cgpara.alignment);
  158. pushsize2 := int_cgsize(tcgsize2size[pushsize] - 2);
  159. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize2],makeregsize(list,GetNextReg(r),pushsize2)));
  160. list.concat(taicpu.op_reg(A_PUSH,S_W,makeregsize(list,r,OS_16)));
  161. end
  162. else
  163. begin
  164. cgpara.check_simple_location;
  165. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  166. pushsize:=cgpara.location^.size
  167. else
  168. pushsize:=int_cgsize(cgpara.alignment);
  169. list.concat(taicpu.op_reg(A_PUSH,TCgsize2opsize[pushsize],makeregsize(list,r,pushsize)));
  170. end;
  171. end
  172. else }
  173. begin
  174. if not(tcgsize2size[cgpara.Size] in [1..4]) then
  175. internalerror(2014011101);
  176. hp:=cgpara.location;
  177. i:=0;
  178. while i<tcgsize2size[cgpara.Size] do
  179. begin
  180. if not(assigned(hp)) then
  181. internalerror(2014011102);
  182. inc(i, tcgsize2size[hp^.Size]);
  183. if hp^.Loc=LOC_REGISTER then
  184. begin
  185. load_para_loc(r,hp);
  186. hp:=hp^.Next;
  187. r:=GetNextReg(r);
  188. end
  189. else
  190. begin
  191. load_para_loc(r,hp);
  192. for i2:=1 to tcgsize2size[hp^.Size] do
  193. r:=GetNextReg(r);
  194. hp:=hp^.Next;
  195. end;
  196. end;
  197. if assigned(hp) then
  198. internalerror(2014011103);
  199. end;
  200. end;
  201. procedure tcgz80.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  202. var
  203. i : longint;
  204. hp : PCGParaLocation;
  205. ref: treference;
  206. begin
  207. if not(tcgsize2size[paraloc.Size] in [1..4]) then
  208. internalerror(2014011101);
  209. hp:=paraloc.location;
  210. i:=1;
  211. while i<=tcgsize2size[paraloc.Size] do
  212. begin
  213. if not(assigned(hp)) then
  214. internalerror(2014011105);
  215. //paramanager.allocparaloc(list,hp);
  216. case hp^.loc of
  217. LOC_REGISTER,LOC_CREGISTER:
  218. begin
  219. if (tcgsize2size[hp^.size]<>1) or
  220. (hp^.shiftval<>0) then
  221. internalerror(2015041101);
  222. a_load_const_reg(list,hp^.size,(a shr (8*(i-1))) and $ff,hp^.register);
  223. inc(i,tcgsize2size[hp^.size]);
  224. hp:=hp^.Next;
  225. end;
  226. LOC_REFERENCE,LOC_CREFERENCE:
  227. begin
  228. reference_reset(ref,paraloc.alignment,[]);
  229. ref.base:=hp^.reference.index;
  230. ref.offset:=hp^.reference.offset;
  231. a_load_const_ref(list,hp^.size,a shr (8*(i-1)),ref);
  232. inc(i,tcgsize2size[hp^.size]);
  233. hp:=hp^.Next;
  234. end;
  235. else
  236. internalerror(2002071004);
  237. end;
  238. end;
  239. end;
  240. procedure tcgz80.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  241. var
  242. tmpref, ref: treference;
  243. location: pcgparalocation;
  244. sizeleft: tcgint;
  245. begin
  246. location := paraloc.location;
  247. tmpref := r;
  248. sizeleft := paraloc.intsize;
  249. while assigned(location) do
  250. begin
  251. paramanager.allocparaloc(list,location);
  252. case location^.loc of
  253. LOC_REGISTER,LOC_CREGISTER:
  254. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  255. LOC_REFERENCE:
  256. begin
  257. reference_reset_base(ref,location^.reference.index,location^.reference.offset,ctempposinvalid,paraloc.alignment,[]);
  258. { doubles in softemu mode have a strange order of registers and references }
  259. if location^.size=OS_32 then
  260. g_concatcopy(list,tmpref,ref,4)
  261. else
  262. begin
  263. g_concatcopy(list,tmpref,ref,sizeleft);
  264. if assigned(location^.next) then
  265. internalerror(2005010710);
  266. end;
  267. end;
  268. LOC_VOID:
  269. begin
  270. // nothing to do
  271. end;
  272. else
  273. internalerror(2002081103);
  274. end;
  275. inc(tmpref.offset,tcgsize2size[location^.size]);
  276. dec(sizeleft,tcgsize2size[location^.size]);
  277. location := location^.next;
  278. end;
  279. end;
  280. procedure tcgz80.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  281. var
  282. tmpreg: tregister;
  283. begin
  284. tmpreg:=getaddressregister(list);
  285. a_loadaddr_ref_reg(list,r,tmpreg);
  286. a_load_reg_cgpara(list,OS_ADDR,tmpreg,paraloc);
  287. end;
  288. procedure tcgz80.a_call_name(list : TAsmList;const s : string; weak: boolean);
  289. var
  290. sym: TAsmSymbol;
  291. begin
  292. if weak then
  293. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)
  294. else
  295. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  296. list.concat(taicpu.op_sym(A_CALL,sym));
  297. include(current_procinfo.flags,pi_do_call);
  298. end;
  299. procedure tcgz80.a_call_reg(list : TAsmList;reg: tregister);
  300. var
  301. l : TAsmLabel;
  302. ref : treference;
  303. begin
  304. current_asmdata.getjumplabel(l);
  305. reference_reset(ref,0,[]);
  306. ref.symbol:=l;
  307. list.concat(taicpu.op_ref_reg(A_LD,ref,reg));
  308. list.concat(tai_const.Create_8bit($CD));
  309. list.concat(tai_label.Create(l));
  310. include(current_procinfo.flags,pi_do_call);
  311. end;
  312. procedure tcgz80.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  313. begin
  314. if not(size in [OS_S8,OS_8,OS_S16,OS_16,OS_S32,OS_32]) then
  315. internalerror(2012102403);
  316. a_op_const_reg_internal(list,Op,size,a,reg,NR_NO);
  317. end;
  318. procedure tcgz80.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src, dst : TRegister);
  319. begin
  320. if not(size in [OS_S8,OS_8,OS_S16,OS_16,OS_S32,OS_32]) then
  321. internalerror(2012102401);
  322. a_op_reg_reg_internal(list,Op,size,src,NR_NO,dst,NR_NO);
  323. end;
  324. procedure tcgz80.a_op_reg_reg_internal(list : TAsmList; Op: TOpCG; size: TCGSize; src, srchi, dst, dsthi: TRegister);
  325. var
  326. countreg,
  327. tmpreg: tregister;
  328. i : integer;
  329. instr : taicpu;
  330. paraloc1,paraloc2,paraloc3 : TCGPara;
  331. l1,l2 : tasmlabel;
  332. pd : tprocdef;
  333. procedure NextSrcDst;
  334. begin
  335. if i=5 then
  336. begin
  337. dst:=dsthi;
  338. src:=srchi;
  339. end
  340. else
  341. begin
  342. dst:=GetNextReg(dst);
  343. src:=GetNextReg(src);
  344. end;
  345. end;
  346. { iterates TmpReg through all registers of dst }
  347. procedure NextTmp;
  348. begin
  349. if i=5 then
  350. tmpreg:=dsthi
  351. else
  352. tmpreg:=GetNextReg(tmpreg);
  353. end;
  354. begin
  355. case op of
  356. OP_ADD:
  357. begin
  358. getcpuregister(list,NR_A);
  359. a_load_reg_reg(list,OS_8,OS_8,dst,NR_A);
  360. list.concat(taicpu.op_reg_reg(A_ADD,NR_A,src));
  361. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  362. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  363. begin
  364. for i:=2 to tcgsize2size[size] do
  365. begin
  366. NextSrcDst;
  367. a_load_reg_reg(list,OS_8,OS_8,dst,NR_A);
  368. list.concat(taicpu.op_reg_reg(A_ADC,NR_A,src));
  369. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  370. end;
  371. end;
  372. ungetcpuregister(list,NR_A);
  373. end;
  374. OP_SUB:
  375. begin
  376. getcpuregister(list,NR_A);
  377. a_load_reg_reg(list,OS_8,OS_8,dst,NR_A);
  378. list.concat(taicpu.op_reg_reg(A_SUB,NR_A,src));
  379. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  380. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  381. begin
  382. for i:=2 to tcgsize2size[size] do
  383. begin
  384. NextSrcDst;
  385. a_load_reg_reg(list,OS_8,OS_8,dst,NR_A);
  386. list.concat(taicpu.op_reg_reg(A_SBC,NR_A,src));
  387. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  388. end;
  389. end;
  390. ungetcpuregister(list,NR_A);
  391. end;
  392. OP_NEG:
  393. begin
  394. if src<>dst then
  395. begin
  396. if size in [OS_S64,OS_64] then
  397. begin
  398. a_load_reg_reg(list,OS_32,OS_32,src,dst);
  399. a_load_reg_reg(list,OS_32,OS_32,srchi,dsthi);
  400. end
  401. else
  402. a_load_reg_reg(list,size,size,src,dst);
  403. end;
  404. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  405. begin
  406. tmpreg:=GetNextReg(dst);
  407. for i:=2 to tcgsize2size[size] do
  408. begin
  409. list.concat(taicpu.op_reg(A_CPL,tmpreg));
  410. NextTmp;
  411. end;
  412. list.concat(taicpu.op_reg(A_NEG,dst));
  413. tmpreg:=GetNextReg(dst);
  414. for i:=2 to tcgsize2size[size] do
  415. begin
  416. list.concat(taicpu.op_reg_const(A_SBC,tmpreg,-1));
  417. NextTmp;
  418. end;
  419. end;
  420. end;
  421. OP_NOT:
  422. begin
  423. getcpuregister(list,NR_A);
  424. for i:=tcgsize2size[size] downto 1 do
  425. begin
  426. a_load_reg_reg(list,OS_8,OS_8,src,NR_A);
  427. list.concat(taicpu.op_none(A_CPL));
  428. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  429. if i<>1 then
  430. NextSrcDst;
  431. end;
  432. ungetcpuregister(list,NR_A);
  433. end;
  434. OP_MUL,OP_IMUL:
  435. { special stuff, needs separate handling inside code
  436. generator }
  437. internalerror(2017032604);
  438. OP_DIV,OP_IDIV:
  439. { special stuff, needs separate handling inside code
  440. generator }
  441. internalerror(2017032604);
  442. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  443. begin
  444. //current_asmdata.getjumplabel(l1);
  445. //current_asmdata.getjumplabel(l2);
  446. //countreg:=getintregister(list,OS_8);
  447. //a_load_reg_reg(list,size,OS_8,src,countreg);
  448. //list.concat(taicpu.op_reg(A_TST,countreg));
  449. //a_jmp_flags(list,F_EQ,l2);
  450. //cg.a_label(list,l1);
  451. //case op of
  452. // OP_SHR:
  453. // list.concat(taicpu.op_reg(A_LSR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  454. // OP_SHL:
  455. // list.concat(taicpu.op_reg(A_LSL,dst));
  456. // OP_SAR:
  457. // list.concat(taicpu.op_reg(A_ASR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  458. // OP_ROR:
  459. // begin
  460. // { load carry? }
  461. // if not(size in [OS_8,OS_S8]) then
  462. // begin
  463. // list.concat(taicpu.op_none(A_CLC));
  464. // list.concat(taicpu.op_reg_const(A_SBRC,src,0));
  465. // list.concat(taicpu.op_none(A_SEC));
  466. // end;
  467. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  468. // end;
  469. // OP_ROL:
  470. // begin
  471. // { load carry? }
  472. // if not(size in [OS_8,OS_S8]) then
  473. // begin
  474. // list.concat(taicpu.op_none(A_CLC));
  475. // list.concat(taicpu.op_reg_const(A_SBRC,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1),7));
  476. // list.concat(taicpu.op_none(A_SEC));
  477. // end;
  478. // list.concat(taicpu.op_reg(A_ROL,dst))
  479. // end;
  480. // else
  481. // internalerror(2011030901);
  482. //end;
  483. //if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  484. // begin
  485. // for i:=2 to tcgsize2size[size] do
  486. // begin
  487. // case op of
  488. // OP_ROR,
  489. // OP_SHR:
  490. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i)));
  491. // OP_ROL,
  492. // OP_SHL:
  493. // list.concat(taicpu.op_reg(A_ROL,GetOffsetReg64(dst,dsthi,i-1)));
  494. // OP_SAR:
  495. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i)));
  496. // else
  497. // internalerror(2011030902);
  498. // end;
  499. // end;
  500. // end;
  501. //
  502. //list.concat(taicpu.op_reg(A_DEC,countreg));
  503. //a_jmp_flags(list,F_NE,l1);
  504. //// keep registers alive
  505. //list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  506. //cg.a_label(list,l2);
  507. end;
  508. OP_AND,OP_OR,OP_XOR:
  509. begin
  510. getcpuregister(list,NR_A);
  511. for i:=tcgsize2size[size] downto 1 do
  512. begin
  513. a_load_reg_reg(list,OS_8,OS_8,dst,NR_A);
  514. list.concat(taicpu.op_reg_reg(topcg2asmop[op],NR_A,src));
  515. a_load_reg_reg(list,OS_8,OS_8,NR_A,dst);
  516. if i<>1 then
  517. NextSrcDst;
  518. end;
  519. ungetcpuregister(list,NR_A);
  520. end;
  521. else
  522. internalerror(2011022004);
  523. end;
  524. end;
  525. procedure tcgz80.a_op_const_reg_internal(list: TAsmList; Op: TOpCG;
  526. size: TCGSize; a: tcgint; reg, reghi: TRegister);
  527. var
  528. mask : qword;
  529. shift : byte;
  530. i,j : byte;
  531. tmpreg : tregister;
  532. tmpreg64 : tregister64;
  533. procedure NextReg;
  534. begin
  535. if i=5 then
  536. reg:=reghi
  537. else
  538. reg:=GetNextReg(reg);
  539. end;
  540. var
  541. curvalue : byte;
  542. begin
  543. optimize_op_const(size,op,a);
  544. mask:=$ff;
  545. shift:=0;
  546. case op of
  547. OP_NONE:
  548. begin
  549. { Opcode is optimized away }
  550. end;
  551. OP_MOVE:
  552. begin
  553. { Optimized, replaced with a simple load }
  554. a_load_const_reg(list,size,a,reg);
  555. end;
  556. OP_OR:
  557. begin
  558. //for i:=1 to tcgsize2size[size] do
  559. // begin
  560. // if ((qword(a) and mask) shr shift)<>0 then
  561. // list.concat(taicpu.op_reg_const(A_ORI,reg,(qword(a) and mask) shr shift));
  562. // NextReg;
  563. // mask:=mask shl 8;
  564. // inc(shift,8);
  565. // end;
  566. end;
  567. OP_AND:
  568. begin
  569. //for i:=1 to tcgsize2size[size] do
  570. // begin
  571. // if ((qword(a) and mask) shr shift)=0 then
  572. // list.concat(taicpu.op_reg_reg(A_MOV,reg,NR_R1))
  573. // else
  574. // list.concat(taicpu.op_reg_const(A_ANDI,reg,(qword(a) and mask) shr shift));
  575. // NextReg;
  576. // mask:=mask shl 8;
  577. // inc(shift,8);
  578. // end;
  579. end;
  580. OP_SUB:
  581. begin
  582. //if ((a and mask)=1) and (tcgsize2size[size]=1) then
  583. // list.concat(taicpu.op_reg(A_DEC,reg))
  584. //else
  585. // list.concat(taicpu.op_reg_const(A_SUBI,reg,a and mask));
  586. //if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  587. // begin
  588. // for i:=2 to tcgsize2size[size] do
  589. // begin
  590. // NextReg;
  591. // mask:=mask shl 8;
  592. // inc(shift,8);
  593. // curvalue:=(qword(a) and mask) shr shift;
  594. // { decrease pressure on upper half of registers by using SBC ...,R1 instead
  595. // of SBCI ...,0 }
  596. // if curvalue=0 then
  597. // list.concat(taicpu.op_reg_reg(A_SBC,reg,NR_R1))
  598. // else
  599. // list.concat(taicpu.op_reg_const(A_SBCI,reg,curvalue));
  600. // end;
  601. // end;
  602. end;
  603. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  604. begin
  605. //if a*tcgsize2size[size]<=8 then
  606. // begin
  607. // for j:=1 to a do
  608. // begin
  609. // case op of
  610. // OP_SHR:
  611. // list.concat(taicpu.op_reg(A_LSR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  612. // OP_SHL:
  613. // list.concat(taicpu.op_reg(A_LSL,reg));
  614. // OP_SAR:
  615. // list.concat(taicpu.op_reg(A_ASR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  616. // OP_ROR:
  617. // begin
  618. // { load carry? }
  619. // if not(size in [OS_8,OS_S8]) then
  620. // begin
  621. // list.concat(taicpu.op_none(A_CLC));
  622. // list.concat(taicpu.op_reg_const(A_SBRC,reg,0));
  623. // list.concat(taicpu.op_none(A_SEC));
  624. // end;
  625. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  626. // end;
  627. // OP_ROL:
  628. // begin
  629. // { load carry? }
  630. // if not(size in [OS_8,OS_S8]) then
  631. // begin
  632. // list.concat(taicpu.op_none(A_CLC));
  633. // list.concat(taicpu.op_reg_const(A_SBRC,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1),7));
  634. // list.concat(taicpu.op_none(A_SEC));
  635. // end;
  636. // list.concat(taicpu.op_reg(A_ROL,reg))
  637. // end;
  638. // else
  639. // internalerror(2011030901);
  640. // end;
  641. // if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  642. // begin
  643. // for i:=2 to tcgsize2size[size] do
  644. // begin
  645. // case op of
  646. // OP_ROR,
  647. // OP_SHR:
  648. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-i)));
  649. // OP_ROL,
  650. // OP_SHL:
  651. // list.concat(taicpu.op_reg(A_ROL,GetOffsetReg64(reg,reghi,i-1)));
  652. // OP_SAR:
  653. // list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-i)));
  654. // else
  655. // internalerror(2011030902);
  656. // end;
  657. // end;
  658. // end;
  659. // end;
  660. // end
  661. //else
  662. // begin
  663. // tmpreg:=getintregister(list,size);
  664. // a_load_const_reg(list,size,a,tmpreg);
  665. // a_op_reg_reg(list,op,size,tmpreg,reg);
  666. // end;
  667. end;
  668. OP_ADD:
  669. begin
  670. //curvalue:=a and mask;
  671. //if curvalue=0 then
  672. // list.concat(taicpu.op_reg_reg(A_ADD,reg,NR_R1))
  673. //else if (curvalue=1) and (tcgsize2size[size]=1) then
  674. // list.concat(taicpu.op_reg(A_INC,reg))
  675. //else
  676. // begin
  677. // tmpreg:=getintregister(list,OS_8);
  678. // a_load_const_reg(list,OS_8,curvalue,tmpreg);
  679. // list.concat(taicpu.op_reg_reg(A_ADD,reg,tmpreg));
  680. // end;
  681. //if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  682. // begin
  683. // for i:=2 to tcgsize2size[size] do
  684. // begin
  685. // NextReg;
  686. // mask:=mask shl 8;
  687. // inc(shift,8);
  688. // curvalue:=(qword(a) and mask) shr shift;
  689. // { decrease pressure on upper half of registers by using ADC ...,R1 instead
  690. // of ADD ...,0 }
  691. // if curvalue=0 then
  692. // list.concat(taicpu.op_reg_reg(A_ADC,reg,NR_R1))
  693. // else
  694. // begin
  695. // tmpreg:=getintregister(list,OS_8);
  696. // a_load_const_reg(list,OS_8,curvalue,tmpreg);
  697. // list.concat(taicpu.op_reg_reg(A_ADC,reg,tmpreg));
  698. // end;
  699. // end;
  700. // end;
  701. end;
  702. else
  703. begin
  704. if size in [OS_64,OS_S64] then
  705. begin
  706. tmpreg64.reglo:=getintregister(list,OS_32);
  707. tmpreg64.reghi:=getintregister(list,OS_32);
  708. cg64.a_load64_const_reg(list,a,tmpreg64);
  709. cg64.a_op64_reg_reg(list,op,size,tmpreg64,joinreg64(reg,reghi));
  710. end
  711. else
  712. begin
  713. {$if 0}
  714. { code not working yet }
  715. if (op=OP_SAR) and (a=31) and (size in [OS_32,OS_S32]) then
  716. begin
  717. tmpreg:=reg;
  718. for i:=1 to 4 do
  719. begin
  720. list.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_R1));
  721. tmpreg:=GetNextReg(tmpreg);
  722. end;
  723. end
  724. else
  725. {$endif}
  726. begin
  727. tmpreg:=getintregister(list,size);
  728. a_load_const_reg(list,size,a,tmpreg);
  729. a_op_reg_reg(list,op,size,tmpreg,reg);
  730. end;
  731. end;
  732. end;
  733. end;
  734. end;
  735. procedure tcgz80.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  736. var
  737. mask : qword;
  738. shift : byte;
  739. i : byte;
  740. begin
  741. mask:=$ff;
  742. shift:=0;
  743. for i:=tcgsize2size[size] downto 1 do
  744. begin
  745. list.Concat(taicpu.op_reg_const(A_LD,reg,(qword(a) and mask) shr shift));
  746. if i<>1 then
  747. begin
  748. mask:=mask shl 8;
  749. inc(shift,8);
  750. reg:=GetNextReg(reg);
  751. end;
  752. end;
  753. end;
  754. procedure tcgz80.maybegetcpuregister(list:tasmlist;reg : tregister);
  755. begin
  756. { allocate the register only, if a cpu register is passed }
  757. if getsupreg(reg)<first_int_imreg then
  758. getcpuregister(list,reg);
  759. end;
  760. function tcgz80.normalize_ref(list:TAsmList;ref: treference;tmpreg : tregister) : treference;
  761. var
  762. tmpref : treference;
  763. l : tasmlabel;
  764. begin
  765. Result:=ref;
  766. //
  767. // if ref.addressmode<>AM_UNCHANGED then
  768. // internalerror(2011021701);
  769. //
  770. // { Be sure to have a base register }
  771. // if (ref.base=NR_NO) then
  772. // begin
  773. // { only symbol+offset? }
  774. // if ref.index=NR_NO then
  775. // exit;
  776. // ref.base:=ref.index;
  777. // ref.index:=NR_NO;
  778. // end;
  779. //
  780. // { can we take advantage of adiw/sbiw? }
  781. // if (current_settings.cputype>=cpu_avr2) and not(assigned(ref.symbol)) and (ref.offset<>0) and (ref.offset>=-63) and (ref.offset<=63) and
  782. // ((tmpreg=NR_R24) or (tmpreg=NR_R26) or (tmpreg=NR_R28) or (tmpreg=NR_R30)) and (ref.base<>NR_NO) then
  783. // begin
  784. // maybegetcpuregister(list,tmpreg);
  785. // emit_mov(list,tmpreg,ref.base);
  786. // maybegetcpuregister(list,GetNextReg(tmpreg));
  787. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  788. // if ref.index<>NR_NO then
  789. // begin
  790. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  791. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  792. // end;
  793. // if ref.offset>0 then
  794. // list.concat(taicpu.op_reg_const(A_ADIW,tmpreg,ref.offset))
  795. // else
  796. // list.concat(taicpu.op_reg_const(A_SBIW,tmpreg,-ref.offset));
  797. // ref.offset:=0;
  798. // ref.base:=tmpreg;
  799. // ref.index:=NR_NO;
  800. // end
  801. // else if assigned(ref.symbol) or (ref.offset<>0) then
  802. // begin
  803. // reference_reset(tmpref,0,[]);
  804. // tmpref.symbol:=ref.symbol;
  805. // tmpref.offset:=ref.offset;
  806. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  807. // tmpref.refaddr:=addr_lo8_gs
  808. // else
  809. // tmpref.refaddr:=addr_lo8;
  810. // maybegetcpuregister(list,tmpreg);
  811. // list.concat(taicpu.op_reg_ref(A_LDI,tmpreg,tmpref));
  812. //
  813. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  814. // tmpref.refaddr:=addr_hi8_gs
  815. // else
  816. // tmpref.refaddr:=addr_hi8;
  817. // maybegetcpuregister(list,GetNextReg(tmpreg));
  818. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(tmpreg),tmpref));
  819. //
  820. // if (ref.base<>NR_NO) then
  821. // begin
  822. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.base));
  823. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.base)));
  824. // end;
  825. // if (ref.index<>NR_NO) then
  826. // begin
  827. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  828. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  829. // end;
  830. // ref.symbol:=nil;
  831. // ref.offset:=0;
  832. // ref.base:=tmpreg;
  833. // ref.index:=NR_NO;
  834. // end
  835. // else if (ref.base<>NR_NO) and (ref.index<>NR_NO) then
  836. // begin
  837. // maybegetcpuregister(list,tmpreg);
  838. // emit_mov(list,tmpreg,ref.base);
  839. // maybegetcpuregister(list,GetNextReg(tmpreg));
  840. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  841. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  842. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  843. // ref.base:=tmpreg;
  844. // ref.index:=NR_NO;
  845. // end
  846. // else if (ref.base<>NR_NO) then
  847. // begin
  848. // maybegetcpuregister(list,tmpreg);
  849. // emit_mov(list,tmpreg,ref.base);
  850. // maybegetcpuregister(list,GetNextReg(tmpreg));
  851. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  852. // ref.base:=tmpreg;
  853. // ref.index:=NR_NO;
  854. // end
  855. // else if (ref.index<>NR_NO) then
  856. // begin
  857. // maybegetcpuregister(list,tmpreg);
  858. // emit_mov(list,tmpreg,ref.index);
  859. // maybegetcpuregister(list,GetNextReg(tmpreg));
  860. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.index));
  861. // ref.base:=tmpreg;
  862. // ref.index:=NR_NO;
  863. // end;
  864. Result:=ref;
  865. end;
  866. procedure tcgz80.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  867. var
  868. href : treference;
  869. i : integer;
  870. begin
  871. href:=Ref;
  872. { ensure, href.base contains a valid register if there is any register used }
  873. if href.base=NR_NO then
  874. begin
  875. href.base:=href.index;
  876. href.index:=NR_NO;
  877. end;
  878. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  879. internalerror(2011021307);
  880. if fromsize=tosize then
  881. begin
  882. getcpuregister(list,NR_A);
  883. for i:=tcgsize2size[fromsize] downto 1 do
  884. begin
  885. a_load_reg_reg(list,OS_8,OS_8,reg,NR_A);
  886. list.concat(taicpu.op_ref_reg(A_LD,href,NR_A));
  887. if i<>1 then
  888. begin
  889. inc(href.offset);
  890. reg:=GetNextReg(reg);
  891. end;
  892. end;
  893. ungetcpuregister(list,NR_A);
  894. end
  895. else
  896. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_ref')));
  897. end;
  898. procedure tcgz80.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;
  899. const Ref : treference;reg : tregister);
  900. var
  901. href : treference;
  902. i : integer;
  903. begin
  904. href:=Ref;
  905. { ensure, href.base contains a valid register if there is any register used }
  906. if href.base=NR_NO then
  907. begin
  908. href.base:=href.index;
  909. href.index:=NR_NO;
  910. end;
  911. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  912. internalerror(2011021307);
  913. if tosize=fromsize then
  914. begin
  915. getcpuregister(list,NR_A);
  916. for i:=tcgsize2size[fromsize] downto 1 do
  917. begin
  918. list.concat(taicpu.op_reg_ref(A_LD,NR_A,href));
  919. a_load_reg_reg(list,OS_8,OS_8,NR_A,reg);
  920. if i<>1 then
  921. begin
  922. inc(href.offset);
  923. reg:=GetNextReg(reg);
  924. end;
  925. end;
  926. ungetcpuregister(list,NR_A);
  927. end
  928. else
  929. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_ref_reg')));
  930. //conv_done:=false;
  931. //if tosize<>fromsize then
  932. // begin
  933. // conv_done:=true;
  934. // if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  935. // fromsize:=tosize;
  936. // case fromsize of
  937. // OS_8:
  938. // begin
  939. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  940. // for i:=2 to tcgsize2size[tosize] do
  941. // begin
  942. // reg:=GetNextReg(reg);
  943. // emit_mov(list,reg,NR_R1);
  944. // end;
  945. // end;
  946. // OS_S8:
  947. // begin
  948. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  949. // tmpreg:=reg;
  950. //
  951. // if tcgsize2size[tosize]>1 then
  952. // begin
  953. // reg:=GetNextReg(reg);
  954. // emit_mov(list,reg,NR_R1);
  955. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  956. // list.concat(taicpu.op_reg(A_COM,reg));
  957. // tmpreg:=reg;
  958. // for i:=3 to tcgsize2size[tosize] do
  959. // begin
  960. // reg:=GetNextReg(reg);
  961. // emit_mov(list,reg,tmpreg);
  962. // end;
  963. // end;
  964. // end;
  965. // OS_16:
  966. // begin
  967. // if not(QuickRef) then
  968. // href.addressmode:=AM_POSTINCREMENT;
  969. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  970. //
  971. // if QuickRef then
  972. // inc(href.offset);
  973. // href.addressmode:=AM_UNCHANGED;
  974. //
  975. // reg:=GetNextReg(reg);
  976. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  977. //
  978. // for i:=3 to tcgsize2size[tosize] do
  979. // begin
  980. // reg:=GetNextReg(reg);
  981. // emit_mov(list,reg,NR_R1);
  982. // end;
  983. // end;
  984. // OS_S16:
  985. // begin
  986. // if not(QuickRef) then
  987. // href.addressmode:=AM_POSTINCREMENT;
  988. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  989. // if QuickRef then
  990. // inc(href.offset);
  991. // href.addressmode:=AM_UNCHANGED;
  992. //
  993. // reg:=GetNextReg(reg);
  994. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  995. // tmpreg:=reg;
  996. //
  997. // reg:=GetNextReg(reg);
  998. // emit_mov(list,reg,NR_R1);
  999. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  1000. // list.concat(taicpu.op_reg(A_COM,reg));
  1001. // tmpreg:=reg;
  1002. // for i:=4 to tcgsize2size[tosize] do
  1003. // begin
  1004. // reg:=GetNextReg(reg);
  1005. // emit_mov(list,reg,tmpreg);
  1006. // end;
  1007. // end;
  1008. // else
  1009. // conv_done:=false;
  1010. // end;
  1011. // end;
  1012. //if not conv_done then
  1013. // begin
  1014. // for i:=1 to tcgsize2size[fromsize] do
  1015. // begin
  1016. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1017. // href.addressmode:=AM_POSTINCREMENT
  1018. // else
  1019. // href.addressmode:=AM_UNCHANGED;
  1020. //
  1021. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1022. //
  1023. // if QuickRef then
  1024. // inc(href.offset);
  1025. //
  1026. // reg:=GetNextReg(reg);
  1027. // end;
  1028. // end;
  1029. //
  1030. //if not(QuickRef) then
  1031. // begin
  1032. // ungetcpuregister(list,href.base);
  1033. // ungetcpuregister(list,GetNextReg(href.base));
  1034. // end;
  1035. end;
  1036. procedure tcgz80.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1037. var
  1038. conv_done: boolean;
  1039. tmpreg : tregister;
  1040. i : integer;
  1041. begin
  1042. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1043. internalerror(2011021310);
  1044. if tosize=fromsize then
  1045. begin
  1046. if reg1<>reg2 then
  1047. for i:=tcgsize2size[fromsize] downto 1 do
  1048. begin
  1049. emit_mov(list,reg2,reg1);
  1050. if i<>1 then
  1051. begin
  1052. reg1:=GetNextReg(reg1);
  1053. reg2:=GetNextReg(reg2);
  1054. end;
  1055. end;
  1056. end
  1057. else
  1058. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_reg')));
  1059. end;
  1060. procedure tcgz80.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1061. begin
  1062. internalerror(2012010702);
  1063. end;
  1064. procedure tcgz80.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1065. begin
  1066. internalerror(2012010703);
  1067. end;
  1068. procedure tcgz80.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1069. begin
  1070. internalerror(2012010704);
  1071. end;
  1072. { comparison operations }
  1073. procedure tcgz80.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;
  1074. cmp_op : topcmp;a : tcgint;reg : tregister;l : tasmlabel);
  1075. var
  1076. swapped : boolean;
  1077. tmpreg : tregister;
  1078. i : byte;
  1079. begin
  1080. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_const_reg_label')));
  1081. //if a=0 then
  1082. // begin
  1083. // swapped:=false;
  1084. // { swap parameters? }
  1085. // case cmp_op of
  1086. // OC_GT:
  1087. // begin
  1088. // swapped:=true;
  1089. // cmp_op:=OC_LT;
  1090. // end;
  1091. // OC_LTE:
  1092. // begin
  1093. // swapped:=true;
  1094. // cmp_op:=OC_GTE;
  1095. // end;
  1096. // OC_BE:
  1097. // begin
  1098. // swapped:=true;
  1099. // cmp_op:=OC_AE;
  1100. // end;
  1101. // OC_A:
  1102. // begin
  1103. // swapped:=true;
  1104. // cmp_op:=OC_B;
  1105. // end;
  1106. // end;
  1107. //
  1108. // if swapped then
  1109. // list.concat(taicpu.op_reg_reg(A_CP,NR_R1,reg))
  1110. // else
  1111. // list.concat(taicpu.op_reg_reg(A_CP,reg,NR_R1));
  1112. //
  1113. // for i:=2 to tcgsize2size[size] do
  1114. // begin
  1115. // reg:=GetNextReg(reg);
  1116. // if swapped then
  1117. // list.concat(taicpu.op_reg_reg(A_CPC,NR_R1,reg))
  1118. // else
  1119. // list.concat(taicpu.op_reg_reg(A_CPC,reg,NR_R1));
  1120. // end;
  1121. //
  1122. // a_jmp_cond(list,cmp_op,l);
  1123. // end
  1124. //else
  1125. // inherited a_cmp_const_reg_label(list,size,cmp_op,a,reg,l);
  1126. end;
  1127. procedure tcgz80.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;
  1128. cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1129. var
  1130. swapped : boolean;
  1131. tmpreg : tregister;
  1132. i : byte;
  1133. begin
  1134. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_reg_reg_label')));
  1135. //swapped:=false;
  1136. //{ swap parameters? }
  1137. //case cmp_op of
  1138. // OC_GT:
  1139. // begin
  1140. // swapped:=true;
  1141. // cmp_op:=OC_LT;
  1142. // end;
  1143. // OC_LTE:
  1144. // begin
  1145. // swapped:=true;
  1146. // cmp_op:=OC_GTE;
  1147. // end;
  1148. // OC_BE:
  1149. // begin
  1150. // swapped:=true;
  1151. // cmp_op:=OC_AE;
  1152. // end;
  1153. // OC_A:
  1154. // begin
  1155. // swapped:=true;
  1156. // cmp_op:=OC_B;
  1157. // end;
  1158. //end;
  1159. //if swapped then
  1160. // begin
  1161. // tmpreg:=reg1;
  1162. // reg1:=reg2;
  1163. // reg2:=tmpreg;
  1164. // end;
  1165. //list.concat(taicpu.op_reg_reg(A_CP,reg2,reg1));
  1166. //
  1167. //for i:=2 to tcgsize2size[size] do
  1168. // begin
  1169. // reg1:=GetNextReg(reg1);
  1170. // reg2:=GetNextReg(reg2);
  1171. // list.concat(taicpu.op_reg_reg(A_CPC,reg2,reg1));
  1172. // end;
  1173. //
  1174. //a_jmp_cond(list,cmp_op,l);
  1175. end;
  1176. procedure tcgz80.a_jmp_name(list : TAsmList;const s : string);
  1177. var
  1178. ai : taicpu;
  1179. begin
  1180. ai:=taicpu.op_sym(A_JP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1181. ai.is_jmp:=true;
  1182. list.concat(ai);
  1183. end;
  1184. procedure tcgz80.a_jmp_always(list : TAsmList;l: tasmlabel);
  1185. var
  1186. ai : taicpu;
  1187. begin
  1188. ai:=taicpu.op_sym(A_JP,l);
  1189. ai.is_jmp:=true;
  1190. list.concat(ai);
  1191. end;
  1192. procedure tcgz80.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1193. var
  1194. ai : taicpu;
  1195. begin
  1196. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_flags')));
  1197. // ai:=setcondition(taicpu.op_sym(A_BRxx,l),flags_to_cond(f));
  1198. ai.is_jmp:=true;
  1199. list.concat(ai);
  1200. end;
  1201. procedure tcgz80.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1202. var
  1203. l : TAsmLabel;
  1204. tmpflags : TResFlags;
  1205. begin
  1206. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_flags2reg')));
  1207. current_asmdata.getjumplabel(l);
  1208. {
  1209. if flags_to_cond(f) then
  1210. begin
  1211. tmpflags:=f;
  1212. inverse_flags(tmpflags);
  1213. emit_mov(reg,NR_R1);
  1214. a_jmp_flags(list,tmpflags,l);
  1215. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1216. end
  1217. else
  1218. }
  1219. begin
  1220. //list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1221. //a_jmp_flags(list,f,l);
  1222. //emit_mov(list,reg,NR_R1);
  1223. end;
  1224. cg.a_label(list,l);
  1225. end;
  1226. procedure tcgz80.a_adjust_sp(list : TAsmList; value : longint);
  1227. var
  1228. i : integer;
  1229. begin
  1230. //case value of
  1231. // 0:
  1232. // ;
  1233. // {-14..-1:
  1234. // begin
  1235. // if ((-value) mod 2)<>0 then
  1236. // list.concat(taicpu.op_reg(A_PUSH,NR_R0));
  1237. // for i:=1 to (-value) div 2 do
  1238. // list.concat(taicpu.op_const(A_RCALL,0));
  1239. // end;
  1240. // 1..7:
  1241. // begin
  1242. // for i:=1 to value do
  1243. // list.concat(taicpu.op_reg(A_POP,NR_R0));
  1244. // end;}
  1245. // else
  1246. // begin
  1247. // list.concat(taicpu.op_reg_const(A_SUBI,NR_R28,lo(word(-value))));
  1248. // list.concat(taicpu.op_reg_const(A_SBCI,NR_R29,hi(word(-value))));
  1249. // // get SREG
  1250. // list.concat(taicpu.op_reg_const(A_IN,NR_R0,NIO_SREG));
  1251. //
  1252. // // block interrupts
  1253. // list.concat(taicpu.op_none(A_CLI));
  1254. //
  1255. // // write high SP
  1256. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_HI,NR_R29));
  1257. //
  1258. // // release interrupts
  1259. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SREG,NR_R0));
  1260. //
  1261. // // write low SP
  1262. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_LO,NR_R28));
  1263. // end;
  1264. //end;
  1265. end;
  1266. procedure tcgz80.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1267. var
  1268. regs : tcpuregisterset;
  1269. reg : tsuperregister;
  1270. begin
  1271. //if po_interrupt in current_procinfo.procdef.procoptions then
  1272. // begin
  1273. // { check if the framepointer is actually used, this is done here because
  1274. // we have to know the size of the locals (must be 0), avr does not know
  1275. // an sp based stack }
  1276. //
  1277. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1278. // (localsize=0) then
  1279. // current_procinfo.framepointer:=NR_NO;
  1280. //
  1281. // { save int registers,
  1282. // but only if the procedure returns }
  1283. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1284. // regs:=rg[R_INTREGISTER].used_in_proc
  1285. // else
  1286. // regs:=[];
  1287. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1288. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1289. // an outer stackframe }
  1290. // if current_procinfo.framepointer<>NR_NO then
  1291. // regs:=regs+[RS_R28,RS_R29];
  1292. //
  1293. // regs:=regs+[RS_R0];
  1294. //
  1295. // for reg:=RS_R31 downto RS_R0 do
  1296. // if reg in regs then
  1297. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1298. //
  1299. // { Save SREG }
  1300. // list.concat(taicpu.op_reg_const(A_IN, NR_R0, $3F));
  1301. // list.concat(taicpu.op_reg(A_PUSH, NR_R0));
  1302. //
  1303. // if current_procinfo.framepointer<>NR_NO then
  1304. // begin
  1305. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1306. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1307. // a_adjust_sp(list,-localsize);
  1308. // end;
  1309. // end
  1310. //else if not(nostackframe) then
  1311. // begin
  1312. // { check if the framepointer is actually used, this is done here because
  1313. // we have to know the size of the locals (must be 0), avr does not know
  1314. // an sp based stack }
  1315. //
  1316. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1317. // (localsize=0) then
  1318. // current_procinfo.framepointer:=NR_NO;
  1319. //
  1320. // { save int registers,
  1321. // but only if the procedure returns }
  1322. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1323. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)
  1324. // else
  1325. // regs:=[];
  1326. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1327. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1328. // an outer stackframe }
  1329. // if current_procinfo.framepointer<>NR_NO then
  1330. // regs:=regs+[RS_R28,RS_R29];
  1331. //
  1332. // for reg:=RS_R31 downto RS_R0 do
  1333. // if reg in regs then
  1334. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1335. //
  1336. // if current_procinfo.framepointer<>NR_NO then
  1337. // begin
  1338. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1339. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1340. // a_adjust_sp(list,-localsize);
  1341. // end;
  1342. // end;
  1343. end;
  1344. procedure tcgz80.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1345. var
  1346. regs : tcpuregisterset;
  1347. reg : TSuperRegister;
  1348. LocalSize : longint;
  1349. begin
  1350. { every byte counts for Z80, so if a subroutine is marked as non-returning, we do
  1351. not generate any exit code, so we really trust the noreturn directive
  1352. }
  1353. if po_noreturn in current_procinfo.procdef.procoptions then
  1354. exit;
  1355. list.concat(taicpu.op_none(A_RET));
  1356. //if po_interrupt in current_procinfo.procdef.procoptions then
  1357. // begin
  1358. // regs:=rg[R_INTREGISTER].used_in_proc;
  1359. // if current_procinfo.framepointer<>NR_NO then
  1360. // begin
  1361. // regs:=regs+[RS_R28,RS_R29];
  1362. // LocalSize:=current_procinfo.calc_stackframe_size;
  1363. // a_adjust_sp(list,LocalSize);
  1364. // end;
  1365. //
  1366. // { Reload SREG }
  1367. // regs:=regs+[RS_R0];
  1368. //
  1369. // list.concat(taicpu.op_reg(A_POP, NR_R0));
  1370. // list.concat(taicpu.op_const_reg(A_OUT, $3F, NR_R0));
  1371. //
  1372. // for reg:=RS_R0 to RS_R31 do
  1373. // if reg in regs then
  1374. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1375. //
  1376. // list.concat(taicpu.op_none(A_RETI));
  1377. // end
  1378. //else if not(nostackframe) then
  1379. // begin
  1380. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1381. // if current_procinfo.framepointer<>NR_NO then
  1382. // begin
  1383. // regs:=regs+[RS_R28,RS_R29];
  1384. // LocalSize:=current_procinfo.calc_stackframe_size;
  1385. // a_adjust_sp(list,LocalSize);
  1386. // end;
  1387. // for reg:=RS_R0 to RS_R31 do
  1388. // if reg in regs then
  1389. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1390. // list.concat(taicpu.op_none(A_RET));
  1391. // end
  1392. //else
  1393. // list.concat(taicpu.op_none(A_RET));
  1394. end;
  1395. procedure tcgz80.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1396. var
  1397. tmpref : treference;
  1398. begin
  1399. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_loadaddr_ref_reg')));
  1400. // if ref.addressmode<>AM_UNCHANGED then
  1401. // internalerror(2011021701);
  1402. //
  1403. //if assigned(ref.symbol) or (ref.offset<>0) then
  1404. // begin
  1405. // reference_reset(tmpref,0,[]);
  1406. // tmpref.symbol:=ref.symbol;
  1407. // tmpref.offset:=ref.offset;
  1408. //
  1409. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1410. // tmpref.refaddr:=addr_lo8_gs
  1411. // else
  1412. // tmpref.refaddr:=addr_lo8;
  1413. // list.concat(taicpu.op_reg_ref(A_LDI,r,tmpref));
  1414. //
  1415. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1416. // tmpref.refaddr:=addr_hi8_gs
  1417. // else
  1418. // tmpref.refaddr:=addr_hi8;
  1419. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(r),tmpref));
  1420. //
  1421. // if (ref.base<>NR_NO) then
  1422. // begin
  1423. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.base));
  1424. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.base)));
  1425. // end;
  1426. // if (ref.index<>NR_NO) then
  1427. // begin
  1428. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1429. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1430. // end;
  1431. // end
  1432. //else if (ref.base<>NR_NO)then
  1433. // begin
  1434. // emit_mov(list,r,ref.base);
  1435. // emit_mov(list,GetNextReg(r),GetNextReg(ref.base));
  1436. // if (ref.index<>NR_NO) then
  1437. // begin
  1438. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1439. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1440. // end;
  1441. // end
  1442. //else if (ref.index<>NR_NO) then
  1443. // begin
  1444. // emit_mov(list,r,ref.index);
  1445. // emit_mov(list,GetNextReg(r),GetNextReg(ref.index));
  1446. // end;
  1447. end;
  1448. procedure tcgz80.fixref(list : TAsmList;var ref : treference);
  1449. begin
  1450. internalerror(2011021320);
  1451. end;
  1452. procedure tcgz80.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1453. var
  1454. paraloc1,paraloc2,paraloc3 : TCGPara;
  1455. pd : tprocdef;
  1456. begin
  1457. pd:=search_system_proc('MOVE');
  1458. paraloc1.init;
  1459. paraloc2.init;
  1460. paraloc3.init;
  1461. {$warning TODO: implement!!!}
  1462. //paramanager.getintparaloc(list,pd,1,paraloc1);
  1463. //paramanager.getintparaloc(list,pd,2,paraloc2);
  1464. //paramanager.getintparaloc(list,pd,3,paraloc3);
  1465. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1466. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1467. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1468. paramanager.freecgpara(list,paraloc3);
  1469. paramanager.freecgpara(list,paraloc2);
  1470. paramanager.freecgpara(list,paraloc1);
  1471. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1472. a_call_name_static(list,'FPC_MOVE');
  1473. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1474. paraloc3.done;
  1475. paraloc2.done;
  1476. paraloc1.done;
  1477. end;
  1478. procedure tcgz80.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1479. var
  1480. countreg,tmpreg : tregister;
  1481. srcref,dstref : treference;
  1482. copysize,countregsize : tcgsize;
  1483. l : TAsmLabel;
  1484. i : longint;
  1485. SrcQuickRef, DestQuickRef : Boolean;
  1486. begin
  1487. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_concatcopy')));
  1488. //if len>16 then
  1489. // begin
  1490. // current_asmdata.getjumplabel(l);
  1491. //
  1492. // reference_reset(srcref,source.alignment,source.volatility);
  1493. // reference_reset(dstref,dest.alignment,source.volatility);
  1494. // srcref.base:=NR_R30;
  1495. // srcref.addressmode:=AM_POSTINCREMENT;
  1496. // dstref.base:=NR_R26;
  1497. // dstref.addressmode:=AM_POSTINCREMENT;
  1498. //
  1499. // copysize:=OS_8;
  1500. // if len<256 then
  1501. // countregsize:=OS_8
  1502. // else if len<65536 then
  1503. // countregsize:=OS_16
  1504. // else
  1505. // internalerror(2011022007);
  1506. // countreg:=getintregister(list,countregsize);
  1507. // a_load_const_reg(list,countregsize,len,countreg);
  1508. // a_loadaddr_ref_reg(list,source,NR_R30);
  1509. //
  1510. // { only base or index register in dest? }
  1511. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1512. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1513. // begin
  1514. // if dest.base<>NR_NO then
  1515. // tmpreg:=dest.base
  1516. // else if dest.index<>NR_NO then
  1517. // tmpreg:=dest.index
  1518. // else
  1519. // internalerror(2016112001);
  1520. // end
  1521. // else
  1522. // begin
  1523. // tmpreg:=getaddressregister(list);
  1524. // a_loadaddr_ref_reg(list,dest,tmpreg);
  1525. // end;
  1526. //
  1527. // { X is used for spilling code so we can load it
  1528. // only by a push/pop sequence, this can be
  1529. // optimized later on by the peephole optimizer
  1530. // }
  1531. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1532. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1533. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1534. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1535. // cg.a_label(list,l);
  1536. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1537. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1538. // list.concat(taicpu.op_reg(A_DEC,countreg));
  1539. // a_jmp_flags(list,F_NE,l);
  1540. // // keep registers alive
  1541. // list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1542. // end
  1543. //else
  1544. // begin
  1545. // SrcQuickRef:=false;
  1546. // DestQuickRef:=false;
  1547. // if not((source.addressmode=AM_UNCHANGED) and
  1548. // (source.symbol=nil) and
  1549. // ((source.base=NR_R28) or
  1550. // (source.base=NR_R30)) and
  1551. // (source.Index=NR_NO) and
  1552. // (source.Offset in [0..64-len])) and
  1553. // not((source.Base=NR_NO) and (source.Index=NR_NO)) then
  1554. // srcref:=normalize_ref(list,source,NR_R30)
  1555. // else
  1556. // begin
  1557. // SrcQuickRef:=true;
  1558. // srcref:=source;
  1559. // end;
  1560. //
  1561. // if not((dest.addressmode=AM_UNCHANGED) and
  1562. // (dest.symbol=nil) and
  1563. // ((dest.base=NR_R28) or
  1564. // (dest.base=NR_R30)) and
  1565. // (dest.Index=NR_No) and
  1566. // (dest.Offset in [0..64-len])) and
  1567. // not((dest.Base=NR_NO) and (dest.Index=NR_NO)) then
  1568. // begin
  1569. // if not(SrcQuickRef) then
  1570. // begin
  1571. // { only base or index register in dest? }
  1572. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1573. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1574. // begin
  1575. // if dest.base<>NR_NO then
  1576. // tmpreg:=dest.base
  1577. // else if dest.index<>NR_NO then
  1578. // tmpreg:=dest.index
  1579. // else
  1580. // internalerror(2016112002);
  1581. // end
  1582. // else
  1583. // tmpreg:=getaddressregister(list);
  1584. //
  1585. // dstref:=normalize_ref(list,dest,tmpreg);
  1586. //
  1587. // { X is used for spilling code so we can load it
  1588. // only by a push/pop sequence, this can be
  1589. // optimized later on by the peephole optimizer
  1590. // }
  1591. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1592. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1593. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1594. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1595. // dstref.base:=NR_R26;
  1596. // end
  1597. // else
  1598. // dstref:=normalize_ref(list,dest,NR_R30);
  1599. // end
  1600. // else
  1601. // begin
  1602. // DestQuickRef:=true;
  1603. // dstref:=dest;
  1604. // end;
  1605. //
  1606. // for i:=1 to len do
  1607. // begin
  1608. // if not(SrcQuickRef) and (i<len) then
  1609. // srcref.addressmode:=AM_POSTINCREMENT
  1610. // else
  1611. // srcref.addressmode:=AM_UNCHANGED;
  1612. //
  1613. // if not(DestQuickRef) and (i<len) then
  1614. // dstref.addressmode:=AM_POSTINCREMENT
  1615. // else
  1616. // dstref.addressmode:=AM_UNCHANGED;
  1617. //
  1618. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1619. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1620. //
  1621. // if SrcQuickRef then
  1622. // inc(srcref.offset);
  1623. // if DestQuickRef then
  1624. // inc(dstref.offset);
  1625. // end;
  1626. // if not(SrcQuickRef) then
  1627. // begin
  1628. // ungetcpuregister(list,srcref.base);
  1629. // ungetcpuregister(list,GetNextReg(srcref.base));
  1630. // end;
  1631. // end;
  1632. end;
  1633. procedure tcgz80.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1634. var
  1635. hl : tasmlabel;
  1636. ai : taicpu;
  1637. cond : TAsmCond;
  1638. begin
  1639. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_overflowCheck')));
  1640. //if not(cs_check_overflow in current_settings.localswitches) then
  1641. // exit;
  1642. //current_asmdata.getjumplabel(hl);
  1643. //if not ((def.typ=pointerdef) or
  1644. // ((def.typ=orddef) and
  1645. // (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1646. // pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1647. // cond:=C_VC
  1648. //else
  1649. // cond:=C_CC;
  1650. //ai:=Taicpu.Op_Sym(A_BRxx,hl);
  1651. //ai.SetCondition(cond);
  1652. //ai.is_jmp:=true;
  1653. //list.concat(ai);
  1654. //
  1655. //a_call_name(list,'FPC_OVERFLOW',false);
  1656. //a_label(list,hl);
  1657. end;
  1658. procedure tcgz80.g_save_registers(list: TAsmList);
  1659. begin
  1660. { this is done by the entry code }
  1661. end;
  1662. procedure tcgz80.g_restore_registers(list: TAsmList);
  1663. begin
  1664. { this is done by the exit code }
  1665. end;
  1666. procedure tcgz80.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1667. var
  1668. ai1,ai2 : taicpu;
  1669. hl : TAsmLabel;
  1670. begin
  1671. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_cond')));
  1672. //ai1:=Taicpu.Op_sym(A_BRxx,l);
  1673. //ai1.is_jmp:=true;
  1674. //hl:=nil;
  1675. //case cond of
  1676. // OC_EQ:
  1677. // ai1.SetCondition(C_EQ);
  1678. // OC_GT:
  1679. // begin
  1680. // { emulate GT }
  1681. // current_asmdata.getjumplabel(hl);
  1682. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1683. // ai2.SetCondition(C_EQ);
  1684. // ai2.is_jmp:=true;
  1685. // list.concat(ai2);
  1686. //
  1687. // ai1.SetCondition(C_GE);
  1688. // end;
  1689. // OC_LT:
  1690. // ai1.SetCondition(C_LT);
  1691. // OC_GTE:
  1692. // ai1.SetCondition(C_GE);
  1693. // OC_LTE:
  1694. // begin
  1695. // { emulate LTE }
  1696. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1697. // ai2.SetCondition(C_EQ);
  1698. // ai2.is_jmp:=true;
  1699. // list.concat(ai2);
  1700. //
  1701. // ai1.SetCondition(C_LT);
  1702. // end;
  1703. // OC_NE:
  1704. // ai1.SetCondition(C_NE);
  1705. // OC_BE:
  1706. // begin
  1707. // { emulate BE }
  1708. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1709. // ai2.SetCondition(C_EQ);
  1710. // ai2.is_jmp:=true;
  1711. // list.concat(ai2);
  1712. //
  1713. // ai1.SetCondition(C_LO);
  1714. // end;
  1715. // OC_B:
  1716. // ai1.SetCondition(C_LO);
  1717. // OC_AE:
  1718. // ai1.SetCondition(C_SH);
  1719. // OC_A:
  1720. // begin
  1721. // { emulate A (unsigned GT) }
  1722. // current_asmdata.getjumplabel(hl);
  1723. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1724. // ai2.SetCondition(C_EQ);
  1725. // ai2.is_jmp:=true;
  1726. // list.concat(ai2);
  1727. //
  1728. // ai1.SetCondition(C_SH);
  1729. // end;
  1730. // else
  1731. // internalerror(2011082501);
  1732. //end;
  1733. //list.concat(ai1);
  1734. //if assigned(hl) then
  1735. // a_label(list,hl);
  1736. end;
  1737. procedure tcgz80.emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  1738. var
  1739. instr: taicpu;
  1740. begin
  1741. instr:=taicpu.op_reg_reg(A_LD,reg2,reg1);
  1742. list.Concat(instr);
  1743. { Notify the register allocator that we have written a move instruction so
  1744. it can try to eliminate it. }
  1745. add_move_instruction(instr);
  1746. end;
  1747. procedure tcg64fz80.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1748. begin
  1749. if not(size in [OS_S64,OS_64]) then
  1750. internalerror(2012102402);
  1751. tcgz80(cg).a_op_reg_reg_internal(list,Op,size,regsrc.reglo,regsrc.reghi,regdst.reglo,regdst.reghi);
  1752. end;
  1753. procedure tcg64fz80.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1754. begin
  1755. tcgz80(cg).a_op_const_reg_internal(list,Op,size,value,reg.reglo,reg.reghi);
  1756. end;
  1757. function GetByteLoc(const loc : tlocation; nr : byte) : tlocation;
  1758. var
  1759. i : Integer;
  1760. begin
  1761. Result:=loc;
  1762. Result.size:=OS_8;
  1763. case loc.loc of
  1764. LOC_REFERENCE,LOC_CREFERENCE:
  1765. inc(Result.reference.offset,nr);
  1766. LOC_REGISTER,LOC_CREGISTER:
  1767. begin
  1768. if nr>=4 then
  1769. Result.register:=Result.register64.reghi;
  1770. nr:=nr mod 4;
  1771. for i:=1 to nr do
  1772. Result.register:=GetNextReg(Result.register);
  1773. end;
  1774. LOC_CONSTANT:
  1775. if loc.size in [OS_64,OS_S64] then
  1776. Result.value:=(Result.value64 shr (nr*8)) and $ff
  1777. else
  1778. Result.value:=(Result.value shr (nr*8)) and $ff;
  1779. else
  1780. Internalerror(2019020902);
  1781. end;
  1782. end;
  1783. procedure create_codegen;
  1784. begin
  1785. cg:=tcgz80.create;
  1786. cg64:=tcg64fz80.create;
  1787. end;
  1788. end.