cgcpu.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_const_reg')));
  724. mask:=$ff;
  725. shift:=0;
  726. for i:=1 to tcgsize2size[size] do
  727. begin
  728. //if ((qword(a) and mask) shr shift)=0 then
  729. // emit_mov(list,reg,NR_R1)
  730. //else
  731. // begin
  732. // getcpuregister(list,NR_R26);
  733. // list.concat(taicpu.op_reg_const(A_LDI,NR_R26,(qword(a) and mask) shr shift));
  734. // a_load_reg_reg(list,OS_8,OS_8,NR_R26,reg);
  735. // ungetcpuregister(list,NR_R26);
  736. // end;
  737. //
  738. //mask:=mask shl 8;
  739. //inc(shift,8);
  740. //reg:=GetNextReg(reg);
  741. end;
  742. end;
  743. procedure tcgz80.maybegetcpuregister(list:tasmlist;reg : tregister);
  744. begin
  745. { allocate the register only, if a cpu register is passed }
  746. if getsupreg(reg)<first_int_imreg then
  747. getcpuregister(list,reg);
  748. end;
  749. function tcgz80.normalize_ref(list:TAsmList;ref: treference;tmpreg : tregister) : treference;
  750. var
  751. tmpref : treference;
  752. l : tasmlabel;
  753. begin
  754. Result:=ref;
  755. //
  756. // if ref.addressmode<>AM_UNCHANGED then
  757. // internalerror(2011021701);
  758. //
  759. // { Be sure to have a base register }
  760. // if (ref.base=NR_NO) then
  761. // begin
  762. // { only symbol+offset? }
  763. // if ref.index=NR_NO then
  764. // exit;
  765. // ref.base:=ref.index;
  766. // ref.index:=NR_NO;
  767. // end;
  768. //
  769. // { can we take advantage of adiw/sbiw? }
  770. // if (current_settings.cputype>=cpu_avr2) and not(assigned(ref.symbol)) and (ref.offset<>0) and (ref.offset>=-63) and (ref.offset<=63) and
  771. // ((tmpreg=NR_R24) or (tmpreg=NR_R26) or (tmpreg=NR_R28) or (tmpreg=NR_R30)) and (ref.base<>NR_NO) then
  772. // begin
  773. // maybegetcpuregister(list,tmpreg);
  774. // emit_mov(list,tmpreg,ref.base);
  775. // maybegetcpuregister(list,GetNextReg(tmpreg));
  776. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  777. // if ref.index<>NR_NO then
  778. // begin
  779. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  780. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  781. // end;
  782. // if ref.offset>0 then
  783. // list.concat(taicpu.op_reg_const(A_ADIW,tmpreg,ref.offset))
  784. // else
  785. // list.concat(taicpu.op_reg_const(A_SBIW,tmpreg,-ref.offset));
  786. // ref.offset:=0;
  787. // ref.base:=tmpreg;
  788. // ref.index:=NR_NO;
  789. // end
  790. // else if assigned(ref.symbol) or (ref.offset<>0) then
  791. // begin
  792. // reference_reset(tmpref,0,[]);
  793. // tmpref.symbol:=ref.symbol;
  794. // tmpref.offset:=ref.offset;
  795. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  796. // tmpref.refaddr:=addr_lo8_gs
  797. // else
  798. // tmpref.refaddr:=addr_lo8;
  799. // maybegetcpuregister(list,tmpreg);
  800. // list.concat(taicpu.op_reg_ref(A_LDI,tmpreg,tmpref));
  801. //
  802. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  803. // tmpref.refaddr:=addr_hi8_gs
  804. // else
  805. // tmpref.refaddr:=addr_hi8;
  806. // maybegetcpuregister(list,GetNextReg(tmpreg));
  807. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(tmpreg),tmpref));
  808. //
  809. // if (ref.base<>NR_NO) then
  810. // begin
  811. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.base));
  812. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.base)));
  813. // end;
  814. // if (ref.index<>NR_NO) then
  815. // begin
  816. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  817. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  818. // end;
  819. // ref.symbol:=nil;
  820. // ref.offset:=0;
  821. // ref.base:=tmpreg;
  822. // ref.index:=NR_NO;
  823. // end
  824. // else if (ref.base<>NR_NO) and (ref.index<>NR_NO) then
  825. // begin
  826. // maybegetcpuregister(list,tmpreg);
  827. // emit_mov(list,tmpreg,ref.base);
  828. // maybegetcpuregister(list,GetNextReg(tmpreg));
  829. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  830. // list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  831. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  832. // ref.base:=tmpreg;
  833. // ref.index:=NR_NO;
  834. // end
  835. // else if (ref.base<>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. // ref.base:=tmpreg;
  842. // ref.index:=NR_NO;
  843. // end
  844. // else if (ref.index<>NR_NO) then
  845. // begin
  846. // maybegetcpuregister(list,tmpreg);
  847. // emit_mov(list,tmpreg,ref.index);
  848. // maybegetcpuregister(list,GetNextReg(tmpreg));
  849. // emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.index));
  850. // ref.base:=tmpreg;
  851. // ref.index:=NR_NO;
  852. // end;
  853. Result:=ref;
  854. end;
  855. procedure tcgz80.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  856. var
  857. href : treference;
  858. conv_done: boolean;
  859. tmpreg : tregister;
  860. i : integer;
  861. QuickRef : Boolean;
  862. begin
  863. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_ref')));
  864. QuickRef:=false;
  865. //href:=Ref;
  866. //{ ensure, href.base contains a valid register if there is any register used }
  867. //if href.base=NR_NO then
  868. // begin
  869. // href.base:=href.index;
  870. // href.index:=NR_NO;
  871. // end;
  872. //
  873. //{ try to use std/sts }
  874. //if not((href.Base=NR_NO) and (href.Index=NR_NO)) then
  875. // begin
  876. // if not((href.addressmode=AM_UNCHANGED) and
  877. // (href.symbol=nil) and
  878. // (href.Index=NR_NO) and
  879. // (href.Offset in [0..64-tcgsize2size[fromsize]])) then
  880. // href:=normalize_ref(list,href,NR_R30)
  881. // else
  882. // begin
  883. // if (href.base<>NR_R28) and (href.base<>NR_R30) then
  884. // begin
  885. // maybegetcpuregister(list,NR_R30);
  886. // emit_mov(list,NR_R30,href.base);
  887. // maybegetcpuregister(list,NR_R31);
  888. // emit_mov(list,NR_R31,GetNextReg(href.base));
  889. // href.base:=NR_R30;
  890. // end;
  891. // QuickRef:=true;
  892. // end;
  893. // end
  894. //else
  895. // QuickRef:=true;
  896. //
  897. //if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  898. // internalerror(2011021307);
  899. //
  900. //conv_done:=false;
  901. //if tosize<>fromsize then
  902. // begin
  903. // conv_done:=true;
  904. // if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  905. // fromsize:=tosize;
  906. // case fromsize of
  907. // OS_8:
  908. // begin
  909. // if not(QuickRef) and (tcgsize2size[tosize]>1) then
  910. // href.addressmode:=AM_POSTINCREMENT;
  911. //
  912. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  913. // for i:=2 to tcgsize2size[tosize] do
  914. // begin
  915. // if QuickRef then
  916. // inc(href.offset);
  917. //
  918. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  919. // href.addressmode:=AM_POSTINCREMENT
  920. // else
  921. // href.addressmode:=AM_UNCHANGED;
  922. //
  923. // list.concat(taicpu.op_ref_reg(GetStore(href),href,NR_R1));
  924. // end;
  925. // end;
  926. // OS_S8:
  927. // begin
  928. // if not(QuickRef) and (tcgsize2size[tosize]>1) then
  929. // href.addressmode:=AM_POSTINCREMENT;
  930. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  931. //
  932. // if tcgsize2size[tosize]>1 then
  933. // begin
  934. // tmpreg:=getintregister(list,OS_8);
  935. // emit_mov(list,tmpreg,NR_R1);
  936. // list.concat(taicpu.op_reg_const(A_SBRC,reg,7));
  937. // list.concat(taicpu.op_reg(A_COM,tmpreg));
  938. // for i:=2 to tcgsize2size[tosize] do
  939. // begin
  940. // if QuickRef then
  941. // inc(href.offset);
  942. //
  943. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  944. // href.addressmode:=AM_POSTINCREMENT
  945. // else
  946. // href.addressmode:=AM_UNCHANGED;
  947. // list.concat(taicpu.op_ref_reg(GetStore(href),href,tmpreg));
  948. // end;
  949. // end;
  950. // end;
  951. // OS_16:
  952. // begin
  953. // if not(QuickRef) and (tcgsize2size[tosize]>1) then
  954. // href.addressmode:=AM_POSTINCREMENT;
  955. //
  956. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  957. // if QuickRef then
  958. // inc(href.offset)
  959. // else if not(QuickRef) and (tcgsize2size[fromsize]>2) then
  960. // href.addressmode:=AM_POSTINCREMENT
  961. // else
  962. // href.addressmode:=AM_UNCHANGED;
  963. //
  964. // reg:=GetNextReg(reg);
  965. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  966. //
  967. // for i:=3 to tcgsize2size[tosize] do
  968. // begin
  969. // if QuickRef then
  970. // inc(href.offset);
  971. //
  972. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  973. // href.addressmode:=AM_POSTINCREMENT
  974. // else
  975. // href.addressmode:=AM_UNCHANGED;
  976. //
  977. // list.concat(taicpu.op_ref_reg(GetStore(href),href,NR_R1));
  978. // end;
  979. // end;
  980. // OS_S16:
  981. // begin
  982. // if not(QuickRef) and (tcgsize2size[tosize]>1) then
  983. // href.addressmode:=AM_POSTINCREMENT;
  984. //
  985. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  986. // if QuickRef then
  987. // inc(href.offset)
  988. // else if not(QuickRef) and (tcgsize2size[fromsize]>2) then
  989. // href.addressmode:=AM_POSTINCREMENT
  990. // else
  991. // href.addressmode:=AM_UNCHANGED;
  992. //
  993. // reg:=GetNextReg(reg);
  994. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  995. //
  996. // if tcgsize2size[tosize]>2 then
  997. // begin
  998. // tmpreg:=getintregister(list,OS_8);
  999. // emit_mov(list,tmpreg,NR_R1);
  1000. // list.concat(taicpu.op_reg_const(A_SBRC,reg,7));
  1001. // list.concat(taicpu.op_reg(A_COM,tmpreg));
  1002. // for i:=3 to tcgsize2size[tosize] do
  1003. // begin
  1004. // if QuickRef then
  1005. // inc(href.offset);
  1006. //
  1007. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1008. // href.addressmode:=AM_POSTINCREMENT
  1009. // else
  1010. // href.addressmode:=AM_UNCHANGED;
  1011. // list.concat(taicpu.op_ref_reg(GetStore(href),href,tmpreg));
  1012. // end;
  1013. // end;
  1014. // end;
  1015. // else
  1016. // conv_done:=false;
  1017. // end;
  1018. // end;
  1019. //if not conv_done then
  1020. // begin
  1021. // for i:=1 to tcgsize2size[fromsize] do
  1022. // begin
  1023. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1024. // href.addressmode:=AM_POSTINCREMENT
  1025. // else
  1026. // href.addressmode:=AM_UNCHANGED;
  1027. //
  1028. // list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1029. //
  1030. // if QuickRef then
  1031. // inc(href.offset);
  1032. //
  1033. // reg:=GetNextReg(reg);
  1034. // end;
  1035. // end;
  1036. //
  1037. //if not(QuickRef) then
  1038. // begin
  1039. // ungetcpuregister(list,href.base);
  1040. // ungetcpuregister(list,GetNextReg(href.base));
  1041. // end;
  1042. end;
  1043. procedure tcgz80.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;
  1044. const Ref : treference;reg : tregister);
  1045. var
  1046. href : treference;
  1047. conv_done: boolean;
  1048. tmpreg : tregister;
  1049. i : integer;
  1050. QuickRef : boolean;
  1051. begin
  1052. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_ref_reg')));
  1053. QuickRef:=false;
  1054. //href:=Ref;
  1055. //{ ensure, href.base contains a valid register if there is any register used }
  1056. //if href.base=NR_NO then
  1057. // begin
  1058. // href.base:=href.index;
  1059. // href.index:=NR_NO;
  1060. // end;
  1061. //
  1062. //{ try to use ldd/lds }
  1063. //if not((href.Base=NR_NO) and (href.Index=NR_NO)) then
  1064. // begin
  1065. // if not((href.addressmode=AM_UNCHANGED) and
  1066. // (href.symbol=nil) and
  1067. // (href.Index=NR_NO) and
  1068. // (href.Offset in [0..64-tcgsize2size[fromsize]])) then
  1069. // href:=normalize_ref(list,href,NR_R30)
  1070. // else
  1071. // begin
  1072. // if (href.base<>NR_R28) and (href.base<>NR_R30) then
  1073. // begin
  1074. // maybegetcpuregister(list,NR_R30);
  1075. // emit_mov(list,NR_R30,href.base);
  1076. // maybegetcpuregister(list,NR_R31);
  1077. // emit_mov(list,NR_R31,GetNextReg(href.base));
  1078. // href.base:=NR_R30;
  1079. // end;
  1080. // QuickRef:=true;
  1081. // end;
  1082. // end
  1083. //else
  1084. // QuickRef:=true;
  1085. //
  1086. //if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1087. // internalerror(2011021307);
  1088. //
  1089. //conv_done:=false;
  1090. //if tosize<>fromsize then
  1091. // begin
  1092. // conv_done:=true;
  1093. // if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1094. // fromsize:=tosize;
  1095. // case fromsize of
  1096. // OS_8:
  1097. // begin
  1098. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1099. // for i:=2 to tcgsize2size[tosize] do
  1100. // begin
  1101. // reg:=GetNextReg(reg);
  1102. // emit_mov(list,reg,NR_R1);
  1103. // end;
  1104. // end;
  1105. // OS_S8:
  1106. // begin
  1107. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1108. // tmpreg:=reg;
  1109. //
  1110. // if tcgsize2size[tosize]>1 then
  1111. // begin
  1112. // reg:=GetNextReg(reg);
  1113. // emit_mov(list,reg,NR_R1);
  1114. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  1115. // list.concat(taicpu.op_reg(A_COM,reg));
  1116. // tmpreg:=reg;
  1117. // for i:=3 to tcgsize2size[tosize] do
  1118. // begin
  1119. // reg:=GetNextReg(reg);
  1120. // emit_mov(list,reg,tmpreg);
  1121. // end;
  1122. // end;
  1123. // end;
  1124. // OS_16:
  1125. // begin
  1126. // if not(QuickRef) then
  1127. // href.addressmode:=AM_POSTINCREMENT;
  1128. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1129. //
  1130. // if QuickRef then
  1131. // inc(href.offset);
  1132. // href.addressmode:=AM_UNCHANGED;
  1133. //
  1134. // reg:=GetNextReg(reg);
  1135. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1136. //
  1137. // for i:=3 to tcgsize2size[tosize] do
  1138. // begin
  1139. // reg:=GetNextReg(reg);
  1140. // emit_mov(list,reg,NR_R1);
  1141. // end;
  1142. // end;
  1143. // OS_S16:
  1144. // begin
  1145. // if not(QuickRef) then
  1146. // href.addressmode:=AM_POSTINCREMENT;
  1147. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1148. // if QuickRef then
  1149. // inc(href.offset);
  1150. // href.addressmode:=AM_UNCHANGED;
  1151. //
  1152. // reg:=GetNextReg(reg);
  1153. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1154. // tmpreg:=reg;
  1155. //
  1156. // reg:=GetNextReg(reg);
  1157. // emit_mov(list,reg,NR_R1);
  1158. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  1159. // list.concat(taicpu.op_reg(A_COM,reg));
  1160. // tmpreg:=reg;
  1161. // for i:=4 to tcgsize2size[tosize] do
  1162. // begin
  1163. // reg:=GetNextReg(reg);
  1164. // emit_mov(list,reg,tmpreg);
  1165. // end;
  1166. // end;
  1167. // else
  1168. // conv_done:=false;
  1169. // end;
  1170. // end;
  1171. //if not conv_done then
  1172. // begin
  1173. // for i:=1 to tcgsize2size[fromsize] do
  1174. // begin
  1175. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1176. // href.addressmode:=AM_POSTINCREMENT
  1177. // else
  1178. // href.addressmode:=AM_UNCHANGED;
  1179. //
  1180. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1181. //
  1182. // if QuickRef then
  1183. // inc(href.offset);
  1184. //
  1185. // reg:=GetNextReg(reg);
  1186. // end;
  1187. // end;
  1188. //
  1189. //if not(QuickRef) then
  1190. // begin
  1191. // ungetcpuregister(list,href.base);
  1192. // ungetcpuregister(list,GetNextReg(href.base));
  1193. // end;
  1194. end;
  1195. procedure tcgz80.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1196. var
  1197. conv_done: boolean;
  1198. tmpreg : tregister;
  1199. i : integer;
  1200. begin
  1201. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_reg')));
  1202. //if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1203. // internalerror(2011021310);
  1204. //
  1205. //conv_done:=false;
  1206. //if tosize<>fromsize then
  1207. // begin
  1208. // conv_done:=true;
  1209. // if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1210. // fromsize:=tosize;
  1211. // case fromsize of
  1212. // OS_8:
  1213. // begin
  1214. // emit_mov(list,reg2,reg1);
  1215. // for i:=2 to tcgsize2size[tosize] do
  1216. // begin
  1217. // reg2:=GetNextReg(reg2);
  1218. // emit_mov(list,reg2,NR_R1);
  1219. // end;
  1220. // end;
  1221. // OS_S8:
  1222. // begin
  1223. // emit_mov(list,reg2,reg1);
  1224. //
  1225. // if tcgsize2size[tosize]>1 then
  1226. // begin
  1227. // reg2:=GetNextReg(reg2);
  1228. // emit_mov(list,reg2,NR_R1);
  1229. // list.concat(taicpu.op_reg_const(A_SBRC,reg1,7));
  1230. // list.concat(taicpu.op_reg(A_COM,reg2));
  1231. // tmpreg:=reg2;
  1232. // for i:=3 to tcgsize2size[tosize] do
  1233. // begin
  1234. // reg2:=GetNextReg(reg2);
  1235. // emit_mov(list,reg2,tmpreg);
  1236. // end;
  1237. // end;
  1238. // end;
  1239. // OS_16:
  1240. // begin
  1241. // emit_mov(list,reg2,reg1);
  1242. //
  1243. // reg1:=GetNextReg(reg1);
  1244. // reg2:=GetNextReg(reg2);
  1245. // emit_mov(list,reg2,reg1);
  1246. //
  1247. // for i:=3 to tcgsize2size[tosize] do
  1248. // begin
  1249. // reg2:=GetNextReg(reg2);
  1250. // emit_mov(list,reg2,NR_R1);
  1251. // end;
  1252. // end;
  1253. // OS_S16:
  1254. // begin
  1255. // emit_mov(list,reg2,reg1);
  1256. //
  1257. // reg1:=GetNextReg(reg1);
  1258. // reg2:=GetNextReg(reg2);
  1259. // emit_mov(list,reg2,reg1);
  1260. //
  1261. // if tcgsize2size[tosize]>2 then
  1262. // begin
  1263. // reg2:=GetNextReg(reg2);
  1264. // emit_mov(list,reg2,NR_R1);
  1265. // list.concat(taicpu.op_reg_const(A_SBRC,reg1,7));
  1266. // list.concat(taicpu.op_reg(A_COM,reg2));
  1267. // tmpreg:=reg2;
  1268. // for i:=4 to tcgsize2size[tosize] do
  1269. // begin
  1270. // reg2:=GetNextReg(reg2);
  1271. // emit_mov(list,reg2,tmpreg);
  1272. // end;
  1273. // end;
  1274. // end;
  1275. // else
  1276. // conv_done:=false;
  1277. // end;
  1278. // end;
  1279. //if not conv_done and (reg1<>reg2) then
  1280. // begin
  1281. // for i:=1 to tcgsize2size[fromsize] do
  1282. // begin
  1283. // emit_mov(list,reg2,reg1);
  1284. // reg1:=GetNextReg(reg1);
  1285. // reg2:=GetNextReg(reg2);
  1286. // end;
  1287. // end;
  1288. end;
  1289. procedure tcgz80.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1290. begin
  1291. internalerror(2012010702);
  1292. end;
  1293. procedure tcgz80.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1294. begin
  1295. internalerror(2012010703);
  1296. end;
  1297. procedure tcgz80.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1298. begin
  1299. internalerror(2012010704);
  1300. end;
  1301. { comparison operations }
  1302. procedure tcgz80.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;
  1303. cmp_op : topcmp;a : tcgint;reg : tregister;l : tasmlabel);
  1304. var
  1305. swapped : boolean;
  1306. tmpreg : tregister;
  1307. i : byte;
  1308. begin
  1309. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_const_reg_label')));
  1310. //if a=0 then
  1311. // begin
  1312. // swapped:=false;
  1313. // { swap parameters? }
  1314. // case cmp_op of
  1315. // OC_GT:
  1316. // begin
  1317. // swapped:=true;
  1318. // cmp_op:=OC_LT;
  1319. // end;
  1320. // OC_LTE:
  1321. // begin
  1322. // swapped:=true;
  1323. // cmp_op:=OC_GTE;
  1324. // end;
  1325. // OC_BE:
  1326. // begin
  1327. // swapped:=true;
  1328. // cmp_op:=OC_AE;
  1329. // end;
  1330. // OC_A:
  1331. // begin
  1332. // swapped:=true;
  1333. // cmp_op:=OC_B;
  1334. // end;
  1335. // end;
  1336. //
  1337. // if swapped then
  1338. // list.concat(taicpu.op_reg_reg(A_CP,NR_R1,reg))
  1339. // else
  1340. // list.concat(taicpu.op_reg_reg(A_CP,reg,NR_R1));
  1341. //
  1342. // for i:=2 to tcgsize2size[size] do
  1343. // begin
  1344. // reg:=GetNextReg(reg);
  1345. // if swapped then
  1346. // list.concat(taicpu.op_reg_reg(A_CPC,NR_R1,reg))
  1347. // else
  1348. // list.concat(taicpu.op_reg_reg(A_CPC,reg,NR_R1));
  1349. // end;
  1350. //
  1351. // a_jmp_cond(list,cmp_op,l);
  1352. // end
  1353. //else
  1354. // inherited a_cmp_const_reg_label(list,size,cmp_op,a,reg,l);
  1355. end;
  1356. procedure tcgz80.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;
  1357. cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1358. var
  1359. swapped : boolean;
  1360. tmpreg : tregister;
  1361. i : byte;
  1362. begin
  1363. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_reg_reg_label')));
  1364. //swapped:=false;
  1365. //{ swap parameters? }
  1366. //case cmp_op of
  1367. // OC_GT:
  1368. // begin
  1369. // swapped:=true;
  1370. // cmp_op:=OC_LT;
  1371. // end;
  1372. // OC_LTE:
  1373. // begin
  1374. // swapped:=true;
  1375. // cmp_op:=OC_GTE;
  1376. // end;
  1377. // OC_BE:
  1378. // begin
  1379. // swapped:=true;
  1380. // cmp_op:=OC_AE;
  1381. // end;
  1382. // OC_A:
  1383. // begin
  1384. // swapped:=true;
  1385. // cmp_op:=OC_B;
  1386. // end;
  1387. //end;
  1388. //if swapped then
  1389. // begin
  1390. // tmpreg:=reg1;
  1391. // reg1:=reg2;
  1392. // reg2:=tmpreg;
  1393. // end;
  1394. //list.concat(taicpu.op_reg_reg(A_CP,reg2,reg1));
  1395. //
  1396. //for i:=2 to tcgsize2size[size] do
  1397. // begin
  1398. // reg1:=GetNextReg(reg1);
  1399. // reg2:=GetNextReg(reg2);
  1400. // list.concat(taicpu.op_reg_reg(A_CPC,reg2,reg1));
  1401. // end;
  1402. //
  1403. //a_jmp_cond(list,cmp_op,l);
  1404. end;
  1405. procedure tcgz80.a_jmp_name(list : TAsmList;const s : string);
  1406. var
  1407. ai : taicpu;
  1408. begin
  1409. ai:=taicpu.op_sym(A_JP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1410. ai.is_jmp:=true;
  1411. list.concat(ai);
  1412. end;
  1413. procedure tcgz80.a_jmp_always(list : TAsmList;l: tasmlabel);
  1414. var
  1415. ai : taicpu;
  1416. begin
  1417. ai:=taicpu.op_sym(A_JP,l);
  1418. ai.is_jmp:=true;
  1419. list.concat(ai);
  1420. end;
  1421. procedure tcgz80.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1422. var
  1423. ai : taicpu;
  1424. begin
  1425. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_flags')));
  1426. // ai:=setcondition(taicpu.op_sym(A_BRxx,l),flags_to_cond(f));
  1427. ai.is_jmp:=true;
  1428. list.concat(ai);
  1429. end;
  1430. procedure tcgz80.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1431. var
  1432. l : TAsmLabel;
  1433. tmpflags : TResFlags;
  1434. begin
  1435. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_flags2reg')));
  1436. current_asmdata.getjumplabel(l);
  1437. {
  1438. if flags_to_cond(f) then
  1439. begin
  1440. tmpflags:=f;
  1441. inverse_flags(tmpflags);
  1442. emit_mov(reg,NR_R1);
  1443. a_jmp_flags(list,tmpflags,l);
  1444. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1445. end
  1446. else
  1447. }
  1448. begin
  1449. //list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1450. //a_jmp_flags(list,f,l);
  1451. //emit_mov(list,reg,NR_R1);
  1452. end;
  1453. cg.a_label(list,l);
  1454. end;
  1455. procedure tcgz80.a_adjust_sp(list : TAsmList; value : longint);
  1456. var
  1457. i : integer;
  1458. begin
  1459. //case value of
  1460. // 0:
  1461. // ;
  1462. // {-14..-1:
  1463. // begin
  1464. // if ((-value) mod 2)<>0 then
  1465. // list.concat(taicpu.op_reg(A_PUSH,NR_R0));
  1466. // for i:=1 to (-value) div 2 do
  1467. // list.concat(taicpu.op_const(A_RCALL,0));
  1468. // end;
  1469. // 1..7:
  1470. // begin
  1471. // for i:=1 to value do
  1472. // list.concat(taicpu.op_reg(A_POP,NR_R0));
  1473. // end;}
  1474. // else
  1475. // begin
  1476. // list.concat(taicpu.op_reg_const(A_SUBI,NR_R28,lo(word(-value))));
  1477. // list.concat(taicpu.op_reg_const(A_SBCI,NR_R29,hi(word(-value))));
  1478. // // get SREG
  1479. // list.concat(taicpu.op_reg_const(A_IN,NR_R0,NIO_SREG));
  1480. //
  1481. // // block interrupts
  1482. // list.concat(taicpu.op_none(A_CLI));
  1483. //
  1484. // // write high SP
  1485. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_HI,NR_R29));
  1486. //
  1487. // // release interrupts
  1488. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SREG,NR_R0));
  1489. //
  1490. // // write low SP
  1491. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_LO,NR_R28));
  1492. // end;
  1493. //end;
  1494. end;
  1495. function tcgz80.GetLoad(const ref: treference) : tasmop;
  1496. begin
  1497. //if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1498. // result:=A_LDS
  1499. //else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1500. // result:=A_LDD
  1501. //else
  1502. // result:=A_LD;
  1503. end;
  1504. function tcgz80.GetStore(const ref: treference) : tasmop;
  1505. begin
  1506. //if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1507. // result:=A_STS
  1508. //else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1509. // result:=A_STD
  1510. //else
  1511. // result:=A_ST;
  1512. end;
  1513. procedure tcgz80.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1514. var
  1515. regs : tcpuregisterset;
  1516. reg : tsuperregister;
  1517. begin
  1518. //if po_interrupt in current_procinfo.procdef.procoptions then
  1519. // begin
  1520. // { check if the framepointer is actually used, this is done here because
  1521. // we have to know the size of the locals (must be 0), avr does not know
  1522. // an sp based stack }
  1523. //
  1524. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1525. // (localsize=0) then
  1526. // current_procinfo.framepointer:=NR_NO;
  1527. //
  1528. // { save int registers,
  1529. // but only if the procedure returns }
  1530. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1531. // regs:=rg[R_INTREGISTER].used_in_proc
  1532. // else
  1533. // regs:=[];
  1534. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1535. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1536. // an outer stackframe }
  1537. // if current_procinfo.framepointer<>NR_NO then
  1538. // regs:=regs+[RS_R28,RS_R29];
  1539. //
  1540. // regs:=regs+[RS_R0];
  1541. //
  1542. // for reg:=RS_R31 downto RS_R0 do
  1543. // if reg in regs then
  1544. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1545. //
  1546. // { Save SREG }
  1547. // list.concat(taicpu.op_reg_const(A_IN, NR_R0, $3F));
  1548. // list.concat(taicpu.op_reg(A_PUSH, NR_R0));
  1549. //
  1550. // if current_procinfo.framepointer<>NR_NO then
  1551. // begin
  1552. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1553. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1554. // a_adjust_sp(list,-localsize);
  1555. // end;
  1556. // end
  1557. //else if not(nostackframe) then
  1558. // begin
  1559. // { check if the framepointer is actually used, this is done here because
  1560. // we have to know the size of the locals (must be 0), avr does not know
  1561. // an sp based stack }
  1562. //
  1563. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1564. // (localsize=0) then
  1565. // current_procinfo.framepointer:=NR_NO;
  1566. //
  1567. // { save int registers,
  1568. // but only if the procedure returns }
  1569. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1570. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)
  1571. // else
  1572. // regs:=[];
  1573. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1574. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1575. // an outer stackframe }
  1576. // if current_procinfo.framepointer<>NR_NO then
  1577. // regs:=regs+[RS_R28,RS_R29];
  1578. //
  1579. // for reg:=RS_R31 downto RS_R0 do
  1580. // if reg in regs then
  1581. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1582. //
  1583. // if current_procinfo.framepointer<>NR_NO then
  1584. // begin
  1585. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1586. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1587. // a_adjust_sp(list,-localsize);
  1588. // end;
  1589. // end;
  1590. end;
  1591. procedure tcgz80.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1592. var
  1593. regs : tcpuregisterset;
  1594. reg : TSuperRegister;
  1595. LocalSize : longint;
  1596. begin
  1597. { every byte counts for Z80, so if a subroutine is marked as non-returning, we do
  1598. not generate any exit code, so we really trust the noreturn directive
  1599. }
  1600. if po_noreturn in current_procinfo.procdef.procoptions then
  1601. exit;
  1602. list.concat(taicpu.op_none(A_RET));
  1603. //if po_interrupt in current_procinfo.procdef.procoptions then
  1604. // begin
  1605. // regs:=rg[R_INTREGISTER].used_in_proc;
  1606. // if current_procinfo.framepointer<>NR_NO then
  1607. // begin
  1608. // regs:=regs+[RS_R28,RS_R29];
  1609. // LocalSize:=current_procinfo.calc_stackframe_size;
  1610. // a_adjust_sp(list,LocalSize);
  1611. // end;
  1612. //
  1613. // { Reload SREG }
  1614. // regs:=regs+[RS_R0];
  1615. //
  1616. // list.concat(taicpu.op_reg(A_POP, NR_R0));
  1617. // list.concat(taicpu.op_const_reg(A_OUT, $3F, NR_R0));
  1618. //
  1619. // for reg:=RS_R0 to RS_R31 do
  1620. // if reg in regs then
  1621. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1622. //
  1623. // list.concat(taicpu.op_none(A_RETI));
  1624. // end
  1625. //else if not(nostackframe) then
  1626. // begin
  1627. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1628. // if current_procinfo.framepointer<>NR_NO then
  1629. // begin
  1630. // regs:=regs+[RS_R28,RS_R29];
  1631. // LocalSize:=current_procinfo.calc_stackframe_size;
  1632. // a_adjust_sp(list,LocalSize);
  1633. // end;
  1634. // for reg:=RS_R0 to RS_R31 do
  1635. // if reg in regs then
  1636. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1637. // list.concat(taicpu.op_none(A_RET));
  1638. // end
  1639. //else
  1640. // list.concat(taicpu.op_none(A_RET));
  1641. end;
  1642. procedure tcgz80.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1643. var
  1644. tmpref : treference;
  1645. begin
  1646. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_loadaddr_ref_reg')));
  1647. // if ref.addressmode<>AM_UNCHANGED then
  1648. // internalerror(2011021701);
  1649. //
  1650. //if assigned(ref.symbol) or (ref.offset<>0) then
  1651. // begin
  1652. // reference_reset(tmpref,0,[]);
  1653. // tmpref.symbol:=ref.symbol;
  1654. // tmpref.offset:=ref.offset;
  1655. //
  1656. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1657. // tmpref.refaddr:=addr_lo8_gs
  1658. // else
  1659. // tmpref.refaddr:=addr_lo8;
  1660. // list.concat(taicpu.op_reg_ref(A_LDI,r,tmpref));
  1661. //
  1662. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1663. // tmpref.refaddr:=addr_hi8_gs
  1664. // else
  1665. // tmpref.refaddr:=addr_hi8;
  1666. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(r),tmpref));
  1667. //
  1668. // if (ref.base<>NR_NO) then
  1669. // begin
  1670. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.base));
  1671. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.base)));
  1672. // end;
  1673. // if (ref.index<>NR_NO) then
  1674. // begin
  1675. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1676. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1677. // end;
  1678. // end
  1679. //else if (ref.base<>NR_NO)then
  1680. // begin
  1681. // emit_mov(list,r,ref.base);
  1682. // emit_mov(list,GetNextReg(r),GetNextReg(ref.base));
  1683. // if (ref.index<>NR_NO) then
  1684. // begin
  1685. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1686. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1687. // end;
  1688. // end
  1689. //else if (ref.index<>NR_NO) then
  1690. // begin
  1691. // emit_mov(list,r,ref.index);
  1692. // emit_mov(list,GetNextReg(r),GetNextReg(ref.index));
  1693. // end;
  1694. end;
  1695. procedure tcgz80.fixref(list : TAsmList;var ref : treference);
  1696. begin
  1697. internalerror(2011021320);
  1698. end;
  1699. procedure tcgz80.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1700. var
  1701. paraloc1,paraloc2,paraloc3 : TCGPara;
  1702. pd : tprocdef;
  1703. begin
  1704. pd:=search_system_proc('MOVE');
  1705. paraloc1.init;
  1706. paraloc2.init;
  1707. paraloc3.init;
  1708. {$warning TODO: implement!!!}
  1709. //paramanager.getintparaloc(list,pd,1,paraloc1);
  1710. //paramanager.getintparaloc(list,pd,2,paraloc2);
  1711. //paramanager.getintparaloc(list,pd,3,paraloc3);
  1712. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1713. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1714. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1715. paramanager.freecgpara(list,paraloc3);
  1716. paramanager.freecgpara(list,paraloc2);
  1717. paramanager.freecgpara(list,paraloc1);
  1718. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1719. a_call_name_static(list,'FPC_MOVE');
  1720. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1721. paraloc3.done;
  1722. paraloc2.done;
  1723. paraloc1.done;
  1724. end;
  1725. procedure tcgz80.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1726. var
  1727. countreg,tmpreg : tregister;
  1728. srcref,dstref : treference;
  1729. copysize,countregsize : tcgsize;
  1730. l : TAsmLabel;
  1731. i : longint;
  1732. SrcQuickRef, DestQuickRef : Boolean;
  1733. begin
  1734. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_concatcopy')));
  1735. //if len>16 then
  1736. // begin
  1737. // current_asmdata.getjumplabel(l);
  1738. //
  1739. // reference_reset(srcref,source.alignment,source.volatility);
  1740. // reference_reset(dstref,dest.alignment,source.volatility);
  1741. // srcref.base:=NR_R30;
  1742. // srcref.addressmode:=AM_POSTINCREMENT;
  1743. // dstref.base:=NR_R26;
  1744. // dstref.addressmode:=AM_POSTINCREMENT;
  1745. //
  1746. // copysize:=OS_8;
  1747. // if len<256 then
  1748. // countregsize:=OS_8
  1749. // else if len<65536 then
  1750. // countregsize:=OS_16
  1751. // else
  1752. // internalerror(2011022007);
  1753. // countreg:=getintregister(list,countregsize);
  1754. // a_load_const_reg(list,countregsize,len,countreg);
  1755. // a_loadaddr_ref_reg(list,source,NR_R30);
  1756. //
  1757. // { only base or index register in dest? }
  1758. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1759. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1760. // begin
  1761. // if dest.base<>NR_NO then
  1762. // tmpreg:=dest.base
  1763. // else if dest.index<>NR_NO then
  1764. // tmpreg:=dest.index
  1765. // else
  1766. // internalerror(2016112001);
  1767. // end
  1768. // else
  1769. // begin
  1770. // tmpreg:=getaddressregister(list);
  1771. // a_loadaddr_ref_reg(list,dest,tmpreg);
  1772. // end;
  1773. //
  1774. // { X is used for spilling code so we can load it
  1775. // only by a push/pop sequence, this can be
  1776. // optimized later on by the peephole optimizer
  1777. // }
  1778. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1779. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1780. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1781. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1782. // cg.a_label(list,l);
  1783. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1784. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1785. // list.concat(taicpu.op_reg(A_DEC,countreg));
  1786. // a_jmp_flags(list,F_NE,l);
  1787. // // keep registers alive
  1788. // list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1789. // end
  1790. //else
  1791. // begin
  1792. // SrcQuickRef:=false;
  1793. // DestQuickRef:=false;
  1794. // if not((source.addressmode=AM_UNCHANGED) and
  1795. // (source.symbol=nil) and
  1796. // ((source.base=NR_R28) or
  1797. // (source.base=NR_R30)) and
  1798. // (source.Index=NR_NO) and
  1799. // (source.Offset in [0..64-len])) and
  1800. // not((source.Base=NR_NO) and (source.Index=NR_NO)) then
  1801. // srcref:=normalize_ref(list,source,NR_R30)
  1802. // else
  1803. // begin
  1804. // SrcQuickRef:=true;
  1805. // srcref:=source;
  1806. // end;
  1807. //
  1808. // if not((dest.addressmode=AM_UNCHANGED) and
  1809. // (dest.symbol=nil) and
  1810. // ((dest.base=NR_R28) or
  1811. // (dest.base=NR_R30)) and
  1812. // (dest.Index=NR_No) and
  1813. // (dest.Offset in [0..64-len])) and
  1814. // not((dest.Base=NR_NO) and (dest.Index=NR_NO)) then
  1815. // begin
  1816. // if not(SrcQuickRef) then
  1817. // begin
  1818. // { only base or index register in dest? }
  1819. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1820. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1821. // begin
  1822. // if dest.base<>NR_NO then
  1823. // tmpreg:=dest.base
  1824. // else if dest.index<>NR_NO then
  1825. // tmpreg:=dest.index
  1826. // else
  1827. // internalerror(2016112002);
  1828. // end
  1829. // else
  1830. // tmpreg:=getaddressregister(list);
  1831. //
  1832. // dstref:=normalize_ref(list,dest,tmpreg);
  1833. //
  1834. // { X is used for spilling code so we can load it
  1835. // only by a push/pop sequence, this can be
  1836. // optimized later on by the peephole optimizer
  1837. // }
  1838. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1839. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1840. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1841. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1842. // dstref.base:=NR_R26;
  1843. // end
  1844. // else
  1845. // dstref:=normalize_ref(list,dest,NR_R30);
  1846. // end
  1847. // else
  1848. // begin
  1849. // DestQuickRef:=true;
  1850. // dstref:=dest;
  1851. // end;
  1852. //
  1853. // for i:=1 to len do
  1854. // begin
  1855. // if not(SrcQuickRef) and (i<len) then
  1856. // srcref.addressmode:=AM_POSTINCREMENT
  1857. // else
  1858. // srcref.addressmode:=AM_UNCHANGED;
  1859. //
  1860. // if not(DestQuickRef) and (i<len) then
  1861. // dstref.addressmode:=AM_POSTINCREMENT
  1862. // else
  1863. // dstref.addressmode:=AM_UNCHANGED;
  1864. //
  1865. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1866. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1867. //
  1868. // if SrcQuickRef then
  1869. // inc(srcref.offset);
  1870. // if DestQuickRef then
  1871. // inc(dstref.offset);
  1872. // end;
  1873. // if not(SrcQuickRef) then
  1874. // begin
  1875. // ungetcpuregister(list,srcref.base);
  1876. // ungetcpuregister(list,GetNextReg(srcref.base));
  1877. // end;
  1878. // end;
  1879. end;
  1880. procedure tcgz80.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1881. var
  1882. hl : tasmlabel;
  1883. ai : taicpu;
  1884. cond : TAsmCond;
  1885. begin
  1886. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_overflowCheck')));
  1887. //if not(cs_check_overflow in current_settings.localswitches) then
  1888. // exit;
  1889. //current_asmdata.getjumplabel(hl);
  1890. //if not ((def.typ=pointerdef) or
  1891. // ((def.typ=orddef) and
  1892. // (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1893. // pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1894. // cond:=C_VC
  1895. //else
  1896. // cond:=C_CC;
  1897. //ai:=Taicpu.Op_Sym(A_BRxx,hl);
  1898. //ai.SetCondition(cond);
  1899. //ai.is_jmp:=true;
  1900. //list.concat(ai);
  1901. //
  1902. //a_call_name(list,'FPC_OVERFLOW',false);
  1903. //a_label(list,hl);
  1904. end;
  1905. procedure tcgz80.g_save_registers(list: TAsmList);
  1906. begin
  1907. { this is done by the entry code }
  1908. end;
  1909. procedure tcgz80.g_restore_registers(list: TAsmList);
  1910. begin
  1911. { this is done by the exit code }
  1912. end;
  1913. procedure tcgz80.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1914. var
  1915. ai1,ai2 : taicpu;
  1916. hl : TAsmLabel;
  1917. begin
  1918. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_cond')));
  1919. //ai1:=Taicpu.Op_sym(A_BRxx,l);
  1920. //ai1.is_jmp:=true;
  1921. //hl:=nil;
  1922. //case cond of
  1923. // OC_EQ:
  1924. // ai1.SetCondition(C_EQ);
  1925. // OC_GT:
  1926. // begin
  1927. // { emulate GT }
  1928. // current_asmdata.getjumplabel(hl);
  1929. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1930. // ai2.SetCondition(C_EQ);
  1931. // ai2.is_jmp:=true;
  1932. // list.concat(ai2);
  1933. //
  1934. // ai1.SetCondition(C_GE);
  1935. // end;
  1936. // OC_LT:
  1937. // ai1.SetCondition(C_LT);
  1938. // OC_GTE:
  1939. // ai1.SetCondition(C_GE);
  1940. // OC_LTE:
  1941. // begin
  1942. // { emulate LTE }
  1943. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1944. // ai2.SetCondition(C_EQ);
  1945. // ai2.is_jmp:=true;
  1946. // list.concat(ai2);
  1947. //
  1948. // ai1.SetCondition(C_LT);
  1949. // end;
  1950. // OC_NE:
  1951. // ai1.SetCondition(C_NE);
  1952. // OC_BE:
  1953. // begin
  1954. // { emulate BE }
  1955. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1956. // ai2.SetCondition(C_EQ);
  1957. // ai2.is_jmp:=true;
  1958. // list.concat(ai2);
  1959. //
  1960. // ai1.SetCondition(C_LO);
  1961. // end;
  1962. // OC_B:
  1963. // ai1.SetCondition(C_LO);
  1964. // OC_AE:
  1965. // ai1.SetCondition(C_SH);
  1966. // OC_A:
  1967. // begin
  1968. // { emulate A (unsigned GT) }
  1969. // current_asmdata.getjumplabel(hl);
  1970. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1971. // ai2.SetCondition(C_EQ);
  1972. // ai2.is_jmp:=true;
  1973. // list.concat(ai2);
  1974. //
  1975. // ai1.SetCondition(C_SH);
  1976. // end;
  1977. // else
  1978. // internalerror(2011082501);
  1979. //end;
  1980. //list.concat(ai1);
  1981. //if assigned(hl) then
  1982. // a_label(list,hl);
  1983. end;
  1984. procedure tcgz80.emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  1985. var
  1986. instr: taicpu;
  1987. begin
  1988. instr:=taicpu.op_reg_reg(A_LD,reg2,reg1);
  1989. list.Concat(instr);
  1990. { Notify the register allocator that we have written a move instruction so
  1991. it can try to eliminate it. }
  1992. add_move_instruction(instr);
  1993. end;
  1994. procedure tcg64favr.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1995. begin
  1996. if not(size in [OS_S64,OS_64]) then
  1997. internalerror(2012102402);
  1998. tcgz80(cg).a_op_reg_reg_internal(list,Op,size,regsrc.reglo,regsrc.reghi,regdst.reglo,regdst.reghi);
  1999. end;
  2000. procedure tcg64favr.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2001. begin
  2002. tcgz80(cg).a_op_const_reg_internal(list,Op,size,value,reg.reglo,reg.reghi);
  2003. end;
  2004. function GetByteLoc(const loc : tlocation; nr : byte) : tlocation;
  2005. var
  2006. i : Integer;
  2007. begin
  2008. Result:=loc;
  2009. Result.size:=OS_8;
  2010. case loc.loc of
  2011. LOC_REFERENCE,LOC_CREFERENCE:
  2012. inc(Result.reference.offset,nr);
  2013. LOC_REGISTER,LOC_CREGISTER:
  2014. begin
  2015. if nr>=4 then
  2016. Result.register:=Result.register64.reghi;
  2017. nr:=nr mod 4;
  2018. for i:=1 to nr do
  2019. Result.register:=GetNextReg(Result.register);
  2020. end;
  2021. LOC_CONSTANT:
  2022. if loc.size in [OS_64,OS_S64] then
  2023. Result.value:=(Result.value64 shr (nr*8)) and $ff
  2024. else
  2025. Result.value:=(Result.value shr (nr*8)) and $ff;
  2026. else
  2027. Internalerror(2019020902);
  2028. end;
  2029. end;
  2030. procedure create_codegen;
  2031. begin
  2032. cg:=tcgz80.create;
  2033. cg64:=tcg64favr.create;
  2034. end;
  2035. end.