cgcpu.pas 74 KB

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