cgcpu.pas 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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. tcg64fz80 = 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. getcpuregister(list,NR_A);
  865. for i:=tcgsize2size[fromsize] downto 1 do
  866. begin
  867. a_load_reg_reg(list,OS_8,OS_8,reg,NR_A);
  868. list.concat(taicpu.op_ref_reg(A_LD,href,NR_A));
  869. if i<>1 then
  870. begin
  871. inc(href.offset);
  872. reg:=GetNextReg(reg);
  873. end;
  874. end;
  875. ungetcpuregister(list,NR_A);
  876. end
  877. else
  878. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_ref')));
  879. end;
  880. procedure tcgz80.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;
  881. const Ref : treference;reg : tregister);
  882. var
  883. href : treference;
  884. i : integer;
  885. begin
  886. href:=Ref;
  887. { ensure, href.base contains a valid register if there is any register used }
  888. if href.base=NR_NO then
  889. begin
  890. href.base:=href.index;
  891. href.index:=NR_NO;
  892. end;
  893. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  894. internalerror(2011021307);
  895. if tosize=fromsize then
  896. begin
  897. getcpuregister(list,NR_A);
  898. for i:=tcgsize2size[fromsize] downto 1 do
  899. begin
  900. list.concat(taicpu.op_reg_ref(A_LD,NR_A,href));
  901. a_load_reg_reg(list,OS_8,OS_8,NR_A,reg);
  902. if i<>1 then
  903. begin
  904. inc(href.offset);
  905. reg:=GetNextReg(reg);
  906. end;
  907. end;
  908. ungetcpuregister(list,NR_A);
  909. end
  910. else
  911. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_ref_reg')));
  912. //conv_done:=false;
  913. //if tosize<>fromsize then
  914. // begin
  915. // conv_done:=true;
  916. // if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  917. // fromsize:=tosize;
  918. // case fromsize of
  919. // OS_8:
  920. // begin
  921. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  922. // for i:=2 to tcgsize2size[tosize] do
  923. // begin
  924. // reg:=GetNextReg(reg);
  925. // emit_mov(list,reg,NR_R1);
  926. // end;
  927. // end;
  928. // OS_S8:
  929. // begin
  930. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  931. // tmpreg:=reg;
  932. //
  933. // if tcgsize2size[tosize]>1 then
  934. // begin
  935. // reg:=GetNextReg(reg);
  936. // emit_mov(list,reg,NR_R1);
  937. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  938. // list.concat(taicpu.op_reg(A_COM,reg));
  939. // tmpreg:=reg;
  940. // for i:=3 to tcgsize2size[tosize] do
  941. // begin
  942. // reg:=GetNextReg(reg);
  943. // emit_mov(list,reg,tmpreg);
  944. // end;
  945. // end;
  946. // end;
  947. // OS_16:
  948. // begin
  949. // if not(QuickRef) then
  950. // href.addressmode:=AM_POSTINCREMENT;
  951. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  952. //
  953. // if QuickRef then
  954. // inc(href.offset);
  955. // href.addressmode:=AM_UNCHANGED;
  956. //
  957. // reg:=GetNextReg(reg);
  958. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  959. //
  960. // for i:=3 to tcgsize2size[tosize] do
  961. // begin
  962. // reg:=GetNextReg(reg);
  963. // emit_mov(list,reg,NR_R1);
  964. // end;
  965. // end;
  966. // OS_S16:
  967. // begin
  968. // if not(QuickRef) then
  969. // href.addressmode:=AM_POSTINCREMENT;
  970. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  971. // if QuickRef then
  972. // inc(href.offset);
  973. // href.addressmode:=AM_UNCHANGED;
  974. //
  975. // reg:=GetNextReg(reg);
  976. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  977. // tmpreg:=reg;
  978. //
  979. // reg:=GetNextReg(reg);
  980. // emit_mov(list,reg,NR_R1);
  981. // list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  982. // list.concat(taicpu.op_reg(A_COM,reg));
  983. // tmpreg:=reg;
  984. // for i:=4 to tcgsize2size[tosize] do
  985. // begin
  986. // reg:=GetNextReg(reg);
  987. // emit_mov(list,reg,tmpreg);
  988. // end;
  989. // end;
  990. // else
  991. // conv_done:=false;
  992. // end;
  993. // end;
  994. //if not conv_done then
  995. // begin
  996. // for i:=1 to tcgsize2size[fromsize] do
  997. // begin
  998. // if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  999. // href.addressmode:=AM_POSTINCREMENT
  1000. // else
  1001. // href.addressmode:=AM_UNCHANGED;
  1002. //
  1003. // list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1004. //
  1005. // if QuickRef then
  1006. // inc(href.offset);
  1007. //
  1008. // reg:=GetNextReg(reg);
  1009. // end;
  1010. // end;
  1011. //
  1012. //if not(QuickRef) then
  1013. // begin
  1014. // ungetcpuregister(list,href.base);
  1015. // ungetcpuregister(list,GetNextReg(href.base));
  1016. // end;
  1017. end;
  1018. procedure tcgz80.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1019. var
  1020. conv_done: boolean;
  1021. tmpreg : tregister;
  1022. i : integer;
  1023. begin
  1024. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1025. internalerror(2011021310);
  1026. if tosize=fromsize then
  1027. begin
  1028. if reg1<>reg2 then
  1029. for i:=tcgsize2size[fromsize] downto 1 do
  1030. begin
  1031. emit_mov(list,reg2,reg1);
  1032. if i<>1 then
  1033. begin
  1034. reg1:=GetNextReg(reg1);
  1035. reg2:=GetNextReg(reg2);
  1036. end;
  1037. end;
  1038. end
  1039. else
  1040. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_reg')));
  1041. end;
  1042. procedure tcgz80.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1043. begin
  1044. internalerror(2012010702);
  1045. end;
  1046. procedure tcgz80.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1047. begin
  1048. internalerror(2012010703);
  1049. end;
  1050. procedure tcgz80.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1051. begin
  1052. internalerror(2012010704);
  1053. end;
  1054. { comparison operations }
  1055. procedure tcgz80.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;
  1056. cmp_op : topcmp;a : tcgint;reg : tregister;l : tasmlabel);
  1057. var
  1058. swapped : boolean;
  1059. tmpreg : tregister;
  1060. i : byte;
  1061. begin
  1062. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_const_reg_label')));
  1063. //if a=0 then
  1064. // begin
  1065. // swapped:=false;
  1066. // { swap parameters? }
  1067. // case cmp_op of
  1068. // OC_GT:
  1069. // begin
  1070. // swapped:=true;
  1071. // cmp_op:=OC_LT;
  1072. // end;
  1073. // OC_LTE:
  1074. // begin
  1075. // swapped:=true;
  1076. // cmp_op:=OC_GTE;
  1077. // end;
  1078. // OC_BE:
  1079. // begin
  1080. // swapped:=true;
  1081. // cmp_op:=OC_AE;
  1082. // end;
  1083. // OC_A:
  1084. // begin
  1085. // swapped:=true;
  1086. // cmp_op:=OC_B;
  1087. // end;
  1088. // end;
  1089. //
  1090. // if swapped then
  1091. // list.concat(taicpu.op_reg_reg(A_CP,NR_R1,reg))
  1092. // else
  1093. // list.concat(taicpu.op_reg_reg(A_CP,reg,NR_R1));
  1094. //
  1095. // for i:=2 to tcgsize2size[size] do
  1096. // begin
  1097. // reg:=GetNextReg(reg);
  1098. // if swapped then
  1099. // list.concat(taicpu.op_reg_reg(A_CPC,NR_R1,reg))
  1100. // else
  1101. // list.concat(taicpu.op_reg_reg(A_CPC,reg,NR_R1));
  1102. // end;
  1103. //
  1104. // a_jmp_cond(list,cmp_op,l);
  1105. // end
  1106. //else
  1107. // inherited a_cmp_const_reg_label(list,size,cmp_op,a,reg,l);
  1108. end;
  1109. procedure tcgz80.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;
  1110. cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1111. var
  1112. swapped : boolean;
  1113. tmpreg : tregister;
  1114. i : byte;
  1115. begin
  1116. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_reg_reg_label')));
  1117. //swapped:=false;
  1118. //{ swap parameters? }
  1119. //case cmp_op of
  1120. // OC_GT:
  1121. // begin
  1122. // swapped:=true;
  1123. // cmp_op:=OC_LT;
  1124. // end;
  1125. // OC_LTE:
  1126. // begin
  1127. // swapped:=true;
  1128. // cmp_op:=OC_GTE;
  1129. // end;
  1130. // OC_BE:
  1131. // begin
  1132. // swapped:=true;
  1133. // cmp_op:=OC_AE;
  1134. // end;
  1135. // OC_A:
  1136. // begin
  1137. // swapped:=true;
  1138. // cmp_op:=OC_B;
  1139. // end;
  1140. //end;
  1141. //if swapped then
  1142. // begin
  1143. // tmpreg:=reg1;
  1144. // reg1:=reg2;
  1145. // reg2:=tmpreg;
  1146. // end;
  1147. //list.concat(taicpu.op_reg_reg(A_CP,reg2,reg1));
  1148. //
  1149. //for i:=2 to tcgsize2size[size] do
  1150. // begin
  1151. // reg1:=GetNextReg(reg1);
  1152. // reg2:=GetNextReg(reg2);
  1153. // list.concat(taicpu.op_reg_reg(A_CPC,reg2,reg1));
  1154. // end;
  1155. //
  1156. //a_jmp_cond(list,cmp_op,l);
  1157. end;
  1158. procedure tcgz80.a_jmp_name(list : TAsmList;const s : string);
  1159. var
  1160. ai : taicpu;
  1161. begin
  1162. ai:=taicpu.op_sym(A_JP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1163. ai.is_jmp:=true;
  1164. list.concat(ai);
  1165. end;
  1166. procedure tcgz80.a_jmp_always(list : TAsmList;l: tasmlabel);
  1167. var
  1168. ai : taicpu;
  1169. begin
  1170. ai:=taicpu.op_sym(A_JP,l);
  1171. ai.is_jmp:=true;
  1172. list.concat(ai);
  1173. end;
  1174. procedure tcgz80.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1175. var
  1176. ai : taicpu;
  1177. begin
  1178. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_flags')));
  1179. // ai:=setcondition(taicpu.op_sym(A_BRxx,l),flags_to_cond(f));
  1180. ai.is_jmp:=true;
  1181. list.concat(ai);
  1182. end;
  1183. procedure tcgz80.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1184. var
  1185. l : TAsmLabel;
  1186. tmpflags : TResFlags;
  1187. begin
  1188. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_flags2reg')));
  1189. current_asmdata.getjumplabel(l);
  1190. {
  1191. if flags_to_cond(f) then
  1192. begin
  1193. tmpflags:=f;
  1194. inverse_flags(tmpflags);
  1195. emit_mov(reg,NR_R1);
  1196. a_jmp_flags(list,tmpflags,l);
  1197. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1198. end
  1199. else
  1200. }
  1201. begin
  1202. //list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1203. //a_jmp_flags(list,f,l);
  1204. //emit_mov(list,reg,NR_R1);
  1205. end;
  1206. cg.a_label(list,l);
  1207. end;
  1208. procedure tcgz80.a_adjust_sp(list : TAsmList; value : longint);
  1209. var
  1210. i : integer;
  1211. begin
  1212. //case value of
  1213. // 0:
  1214. // ;
  1215. // {-14..-1:
  1216. // begin
  1217. // if ((-value) mod 2)<>0 then
  1218. // list.concat(taicpu.op_reg(A_PUSH,NR_R0));
  1219. // for i:=1 to (-value) div 2 do
  1220. // list.concat(taicpu.op_const(A_RCALL,0));
  1221. // end;
  1222. // 1..7:
  1223. // begin
  1224. // for i:=1 to value do
  1225. // list.concat(taicpu.op_reg(A_POP,NR_R0));
  1226. // end;}
  1227. // else
  1228. // begin
  1229. // list.concat(taicpu.op_reg_const(A_SUBI,NR_R28,lo(word(-value))));
  1230. // list.concat(taicpu.op_reg_const(A_SBCI,NR_R29,hi(word(-value))));
  1231. // // get SREG
  1232. // list.concat(taicpu.op_reg_const(A_IN,NR_R0,NIO_SREG));
  1233. //
  1234. // // block interrupts
  1235. // list.concat(taicpu.op_none(A_CLI));
  1236. //
  1237. // // write high SP
  1238. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_HI,NR_R29));
  1239. //
  1240. // // release interrupts
  1241. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SREG,NR_R0));
  1242. //
  1243. // // write low SP
  1244. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_LO,NR_R28));
  1245. // end;
  1246. //end;
  1247. end;
  1248. function tcgz80.GetLoad(const ref: treference) : tasmop;
  1249. begin
  1250. //if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1251. // result:=A_LDS
  1252. //else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1253. // result:=A_LDD
  1254. //else
  1255. // result:=A_LD;
  1256. end;
  1257. function tcgz80.GetStore(const ref: treference) : tasmop;
  1258. begin
  1259. //if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1260. // result:=A_STS
  1261. //else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1262. // result:=A_STD
  1263. //else
  1264. // result:=A_ST;
  1265. end;
  1266. procedure tcgz80.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1267. var
  1268. regs : tcpuregisterset;
  1269. reg : tsuperregister;
  1270. begin
  1271. //if po_interrupt in current_procinfo.procdef.procoptions then
  1272. // begin
  1273. // { check if the framepointer is actually used, this is done here because
  1274. // we have to know the size of the locals (must be 0), avr does not know
  1275. // an sp based stack }
  1276. //
  1277. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1278. // (localsize=0) then
  1279. // current_procinfo.framepointer:=NR_NO;
  1280. //
  1281. // { save int registers,
  1282. // but only if the procedure returns }
  1283. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1284. // regs:=rg[R_INTREGISTER].used_in_proc
  1285. // else
  1286. // regs:=[];
  1287. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1288. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1289. // an outer stackframe }
  1290. // if current_procinfo.framepointer<>NR_NO then
  1291. // regs:=regs+[RS_R28,RS_R29];
  1292. //
  1293. // regs:=regs+[RS_R0];
  1294. //
  1295. // for reg:=RS_R31 downto RS_R0 do
  1296. // if reg in regs then
  1297. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1298. //
  1299. // { Save SREG }
  1300. // list.concat(taicpu.op_reg_const(A_IN, NR_R0, $3F));
  1301. // list.concat(taicpu.op_reg(A_PUSH, NR_R0));
  1302. //
  1303. // if current_procinfo.framepointer<>NR_NO then
  1304. // begin
  1305. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1306. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1307. // a_adjust_sp(list,-localsize);
  1308. // end;
  1309. // end
  1310. //else if not(nostackframe) then
  1311. // begin
  1312. // { check if the framepointer is actually used, this is done here because
  1313. // we have to know the size of the locals (must be 0), avr does not know
  1314. // an sp based stack }
  1315. //
  1316. // if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1317. // (localsize=0) then
  1318. // current_procinfo.framepointer:=NR_NO;
  1319. //
  1320. // { save int registers,
  1321. // but only if the procedure returns }
  1322. // if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1323. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)
  1324. // else
  1325. // regs:=[];
  1326. // { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1327. // even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1328. // an outer stackframe }
  1329. // if current_procinfo.framepointer<>NR_NO then
  1330. // regs:=regs+[RS_R28,RS_R29];
  1331. //
  1332. // for reg:=RS_R31 downto RS_R0 do
  1333. // if reg in regs then
  1334. // list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1335. //
  1336. // if current_procinfo.framepointer<>NR_NO then
  1337. // begin
  1338. // list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  1339. // list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  1340. // a_adjust_sp(list,-localsize);
  1341. // end;
  1342. // end;
  1343. end;
  1344. procedure tcgz80.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1345. var
  1346. regs : tcpuregisterset;
  1347. reg : TSuperRegister;
  1348. LocalSize : longint;
  1349. begin
  1350. { every byte counts for Z80, so if a subroutine is marked as non-returning, we do
  1351. not generate any exit code, so we really trust the noreturn directive
  1352. }
  1353. if po_noreturn in current_procinfo.procdef.procoptions then
  1354. exit;
  1355. list.concat(taicpu.op_none(A_RET));
  1356. //if po_interrupt in current_procinfo.procdef.procoptions then
  1357. // begin
  1358. // regs:=rg[R_INTREGISTER].used_in_proc;
  1359. // if current_procinfo.framepointer<>NR_NO then
  1360. // begin
  1361. // regs:=regs+[RS_R28,RS_R29];
  1362. // LocalSize:=current_procinfo.calc_stackframe_size;
  1363. // a_adjust_sp(list,LocalSize);
  1364. // end;
  1365. //
  1366. // { Reload SREG }
  1367. // regs:=regs+[RS_R0];
  1368. //
  1369. // list.concat(taicpu.op_reg(A_POP, NR_R0));
  1370. // list.concat(taicpu.op_const_reg(A_OUT, $3F, NR_R0));
  1371. //
  1372. // for reg:=RS_R0 to RS_R31 do
  1373. // if reg in regs then
  1374. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1375. //
  1376. // list.concat(taicpu.op_none(A_RETI));
  1377. // end
  1378. //else if not(nostackframe) then
  1379. // begin
  1380. // regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1381. // if current_procinfo.framepointer<>NR_NO then
  1382. // begin
  1383. // regs:=regs+[RS_R28,RS_R29];
  1384. // LocalSize:=current_procinfo.calc_stackframe_size;
  1385. // a_adjust_sp(list,LocalSize);
  1386. // end;
  1387. // for reg:=RS_R0 to RS_R31 do
  1388. // if reg in regs then
  1389. // list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1390. // list.concat(taicpu.op_none(A_RET));
  1391. // end
  1392. //else
  1393. // list.concat(taicpu.op_none(A_RET));
  1394. end;
  1395. procedure tcgz80.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1396. var
  1397. tmpref : treference;
  1398. begin
  1399. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_loadaddr_ref_reg')));
  1400. // if ref.addressmode<>AM_UNCHANGED then
  1401. // internalerror(2011021701);
  1402. //
  1403. //if assigned(ref.symbol) or (ref.offset<>0) then
  1404. // begin
  1405. // reference_reset(tmpref,0,[]);
  1406. // tmpref.symbol:=ref.symbol;
  1407. // tmpref.offset:=ref.offset;
  1408. //
  1409. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1410. // tmpref.refaddr:=addr_lo8_gs
  1411. // else
  1412. // tmpref.refaddr:=addr_lo8;
  1413. // list.concat(taicpu.op_reg_ref(A_LDI,r,tmpref));
  1414. //
  1415. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1416. // tmpref.refaddr:=addr_hi8_gs
  1417. // else
  1418. // tmpref.refaddr:=addr_hi8;
  1419. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(r),tmpref));
  1420. //
  1421. // if (ref.base<>NR_NO) then
  1422. // begin
  1423. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.base));
  1424. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.base)));
  1425. // end;
  1426. // if (ref.index<>NR_NO) then
  1427. // begin
  1428. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1429. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1430. // end;
  1431. // end
  1432. //else if (ref.base<>NR_NO)then
  1433. // begin
  1434. // emit_mov(list,r,ref.base);
  1435. // emit_mov(list,GetNextReg(r),GetNextReg(ref.base));
  1436. // if (ref.index<>NR_NO) then
  1437. // begin
  1438. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1439. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1440. // end;
  1441. // end
  1442. //else if (ref.index<>NR_NO) then
  1443. // begin
  1444. // emit_mov(list,r,ref.index);
  1445. // emit_mov(list,GetNextReg(r),GetNextReg(ref.index));
  1446. // end;
  1447. end;
  1448. procedure tcgz80.fixref(list : TAsmList;var ref : treference);
  1449. begin
  1450. internalerror(2011021320);
  1451. end;
  1452. procedure tcgz80.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1453. var
  1454. paraloc1,paraloc2,paraloc3 : TCGPara;
  1455. pd : tprocdef;
  1456. begin
  1457. pd:=search_system_proc('MOVE');
  1458. paraloc1.init;
  1459. paraloc2.init;
  1460. paraloc3.init;
  1461. {$warning TODO: implement!!!}
  1462. //paramanager.getintparaloc(list,pd,1,paraloc1);
  1463. //paramanager.getintparaloc(list,pd,2,paraloc2);
  1464. //paramanager.getintparaloc(list,pd,3,paraloc3);
  1465. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1466. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1467. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1468. paramanager.freecgpara(list,paraloc3);
  1469. paramanager.freecgpara(list,paraloc2);
  1470. paramanager.freecgpara(list,paraloc1);
  1471. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1472. a_call_name_static(list,'FPC_MOVE');
  1473. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1474. paraloc3.done;
  1475. paraloc2.done;
  1476. paraloc1.done;
  1477. end;
  1478. procedure tcgz80.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1479. var
  1480. countreg,tmpreg : tregister;
  1481. srcref,dstref : treference;
  1482. copysize,countregsize : tcgsize;
  1483. l : TAsmLabel;
  1484. i : longint;
  1485. SrcQuickRef, DestQuickRef : Boolean;
  1486. begin
  1487. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_concatcopy')));
  1488. //if len>16 then
  1489. // begin
  1490. // current_asmdata.getjumplabel(l);
  1491. //
  1492. // reference_reset(srcref,source.alignment,source.volatility);
  1493. // reference_reset(dstref,dest.alignment,source.volatility);
  1494. // srcref.base:=NR_R30;
  1495. // srcref.addressmode:=AM_POSTINCREMENT;
  1496. // dstref.base:=NR_R26;
  1497. // dstref.addressmode:=AM_POSTINCREMENT;
  1498. //
  1499. // copysize:=OS_8;
  1500. // if len<256 then
  1501. // countregsize:=OS_8
  1502. // else if len<65536 then
  1503. // countregsize:=OS_16
  1504. // else
  1505. // internalerror(2011022007);
  1506. // countreg:=getintregister(list,countregsize);
  1507. // a_load_const_reg(list,countregsize,len,countreg);
  1508. // a_loadaddr_ref_reg(list,source,NR_R30);
  1509. //
  1510. // { only base or index register in dest? }
  1511. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1512. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1513. // begin
  1514. // if dest.base<>NR_NO then
  1515. // tmpreg:=dest.base
  1516. // else if dest.index<>NR_NO then
  1517. // tmpreg:=dest.index
  1518. // else
  1519. // internalerror(2016112001);
  1520. // end
  1521. // else
  1522. // begin
  1523. // tmpreg:=getaddressregister(list);
  1524. // a_loadaddr_ref_reg(list,dest,tmpreg);
  1525. // end;
  1526. //
  1527. // { X is used for spilling code so we can load it
  1528. // only by a push/pop sequence, this can be
  1529. // optimized later on by the peephole optimizer
  1530. // }
  1531. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1532. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1533. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1534. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1535. // cg.a_label(list,l);
  1536. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1537. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1538. // list.concat(taicpu.op_reg(A_DEC,countreg));
  1539. // a_jmp_flags(list,F_NE,l);
  1540. // // keep registers alive
  1541. // list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1542. // end
  1543. //else
  1544. // begin
  1545. // SrcQuickRef:=false;
  1546. // DestQuickRef:=false;
  1547. // if not((source.addressmode=AM_UNCHANGED) and
  1548. // (source.symbol=nil) and
  1549. // ((source.base=NR_R28) or
  1550. // (source.base=NR_R30)) and
  1551. // (source.Index=NR_NO) and
  1552. // (source.Offset in [0..64-len])) and
  1553. // not((source.Base=NR_NO) and (source.Index=NR_NO)) then
  1554. // srcref:=normalize_ref(list,source,NR_R30)
  1555. // else
  1556. // begin
  1557. // SrcQuickRef:=true;
  1558. // srcref:=source;
  1559. // end;
  1560. //
  1561. // if not((dest.addressmode=AM_UNCHANGED) and
  1562. // (dest.symbol=nil) and
  1563. // ((dest.base=NR_R28) or
  1564. // (dest.base=NR_R30)) and
  1565. // (dest.Index=NR_No) and
  1566. // (dest.Offset in [0..64-len])) and
  1567. // not((dest.Base=NR_NO) and (dest.Index=NR_NO)) then
  1568. // begin
  1569. // if not(SrcQuickRef) then
  1570. // begin
  1571. // { only base or index register in dest? }
  1572. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1573. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1574. // begin
  1575. // if dest.base<>NR_NO then
  1576. // tmpreg:=dest.base
  1577. // else if dest.index<>NR_NO then
  1578. // tmpreg:=dest.index
  1579. // else
  1580. // internalerror(2016112002);
  1581. // end
  1582. // else
  1583. // tmpreg:=getaddressregister(list);
  1584. //
  1585. // dstref:=normalize_ref(list,dest,tmpreg);
  1586. //
  1587. // { X is used for spilling code so we can load it
  1588. // only by a push/pop sequence, this can be
  1589. // optimized later on by the peephole optimizer
  1590. // }
  1591. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1592. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1593. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1594. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1595. // dstref.base:=NR_R26;
  1596. // end
  1597. // else
  1598. // dstref:=normalize_ref(list,dest,NR_R30);
  1599. // end
  1600. // else
  1601. // begin
  1602. // DestQuickRef:=true;
  1603. // dstref:=dest;
  1604. // end;
  1605. //
  1606. // for i:=1 to len do
  1607. // begin
  1608. // if not(SrcQuickRef) and (i<len) then
  1609. // srcref.addressmode:=AM_POSTINCREMENT
  1610. // else
  1611. // srcref.addressmode:=AM_UNCHANGED;
  1612. //
  1613. // if not(DestQuickRef) and (i<len) then
  1614. // dstref.addressmode:=AM_POSTINCREMENT
  1615. // else
  1616. // dstref.addressmode:=AM_UNCHANGED;
  1617. //
  1618. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1619. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1620. //
  1621. // if SrcQuickRef then
  1622. // inc(srcref.offset);
  1623. // if DestQuickRef then
  1624. // inc(dstref.offset);
  1625. // end;
  1626. // if not(SrcQuickRef) then
  1627. // begin
  1628. // ungetcpuregister(list,srcref.base);
  1629. // ungetcpuregister(list,GetNextReg(srcref.base));
  1630. // end;
  1631. // end;
  1632. end;
  1633. procedure tcgz80.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1634. var
  1635. hl : tasmlabel;
  1636. ai : taicpu;
  1637. cond : TAsmCond;
  1638. begin
  1639. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_overflowCheck')));
  1640. //if not(cs_check_overflow in current_settings.localswitches) then
  1641. // exit;
  1642. //current_asmdata.getjumplabel(hl);
  1643. //if not ((def.typ=pointerdef) or
  1644. // ((def.typ=orddef) and
  1645. // (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1646. // pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1647. // cond:=C_VC
  1648. //else
  1649. // cond:=C_CC;
  1650. //ai:=Taicpu.Op_Sym(A_BRxx,hl);
  1651. //ai.SetCondition(cond);
  1652. //ai.is_jmp:=true;
  1653. //list.concat(ai);
  1654. //
  1655. //a_call_name(list,'FPC_OVERFLOW',false);
  1656. //a_label(list,hl);
  1657. end;
  1658. procedure tcgz80.g_save_registers(list: TAsmList);
  1659. begin
  1660. { this is done by the entry code }
  1661. end;
  1662. procedure tcgz80.g_restore_registers(list: TAsmList);
  1663. begin
  1664. { this is done by the exit code }
  1665. end;
  1666. procedure tcgz80.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1667. var
  1668. ai1,ai2 : taicpu;
  1669. hl : TAsmLabel;
  1670. begin
  1671. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_cond')));
  1672. //ai1:=Taicpu.Op_sym(A_BRxx,l);
  1673. //ai1.is_jmp:=true;
  1674. //hl:=nil;
  1675. //case cond of
  1676. // OC_EQ:
  1677. // ai1.SetCondition(C_EQ);
  1678. // OC_GT:
  1679. // begin
  1680. // { emulate GT }
  1681. // current_asmdata.getjumplabel(hl);
  1682. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1683. // ai2.SetCondition(C_EQ);
  1684. // ai2.is_jmp:=true;
  1685. // list.concat(ai2);
  1686. //
  1687. // ai1.SetCondition(C_GE);
  1688. // end;
  1689. // OC_LT:
  1690. // ai1.SetCondition(C_LT);
  1691. // OC_GTE:
  1692. // ai1.SetCondition(C_GE);
  1693. // OC_LTE:
  1694. // begin
  1695. // { emulate LTE }
  1696. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1697. // ai2.SetCondition(C_EQ);
  1698. // ai2.is_jmp:=true;
  1699. // list.concat(ai2);
  1700. //
  1701. // ai1.SetCondition(C_LT);
  1702. // end;
  1703. // OC_NE:
  1704. // ai1.SetCondition(C_NE);
  1705. // OC_BE:
  1706. // begin
  1707. // { emulate BE }
  1708. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1709. // ai2.SetCondition(C_EQ);
  1710. // ai2.is_jmp:=true;
  1711. // list.concat(ai2);
  1712. //
  1713. // ai1.SetCondition(C_LO);
  1714. // end;
  1715. // OC_B:
  1716. // ai1.SetCondition(C_LO);
  1717. // OC_AE:
  1718. // ai1.SetCondition(C_SH);
  1719. // OC_A:
  1720. // begin
  1721. // { emulate A (unsigned GT) }
  1722. // current_asmdata.getjumplabel(hl);
  1723. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1724. // ai2.SetCondition(C_EQ);
  1725. // ai2.is_jmp:=true;
  1726. // list.concat(ai2);
  1727. //
  1728. // ai1.SetCondition(C_SH);
  1729. // end;
  1730. // else
  1731. // internalerror(2011082501);
  1732. //end;
  1733. //list.concat(ai1);
  1734. //if assigned(hl) then
  1735. // a_label(list,hl);
  1736. end;
  1737. procedure tcgz80.emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  1738. var
  1739. instr: taicpu;
  1740. begin
  1741. instr:=taicpu.op_reg_reg(A_LD,reg2,reg1);
  1742. list.Concat(instr);
  1743. { Notify the register allocator that we have written a move instruction so
  1744. it can try to eliminate it. }
  1745. add_move_instruction(instr);
  1746. end;
  1747. procedure tcg64fz80.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1748. begin
  1749. if not(size in [OS_S64,OS_64]) then
  1750. internalerror(2012102402);
  1751. tcgz80(cg).a_op_reg_reg_internal(list,Op,size,regsrc.reglo,regsrc.reghi,regdst.reglo,regdst.reghi);
  1752. end;
  1753. procedure tcg64fz80.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1754. begin
  1755. tcgz80(cg).a_op_const_reg_internal(list,Op,size,value,reg.reglo,reg.reghi);
  1756. end;
  1757. function GetByteLoc(const loc : tlocation; nr : byte) : tlocation;
  1758. var
  1759. i : Integer;
  1760. begin
  1761. Result:=loc;
  1762. Result.size:=OS_8;
  1763. case loc.loc of
  1764. LOC_REFERENCE,LOC_CREFERENCE:
  1765. inc(Result.reference.offset,nr);
  1766. LOC_REGISTER,LOC_CREGISTER:
  1767. begin
  1768. if nr>=4 then
  1769. Result.register:=Result.register64.reghi;
  1770. nr:=nr mod 4;
  1771. for i:=1 to nr do
  1772. Result.register:=GetNextReg(Result.register);
  1773. end;
  1774. LOC_CONSTANT:
  1775. if loc.size in [OS_64,OS_S64] then
  1776. Result.value:=(Result.value64 shr (nr*8)) and $ff
  1777. else
  1778. Result.value:=(Result.value shr (nr*8)) and $ff;
  1779. else
  1780. Internalerror(2019020902);
  1781. end;
  1782. end;
  1783. procedure create_codegen;
  1784. begin
  1785. cg:=tcgz80.create;
  1786. cg64:=tcg64fz80.create;
  1787. end;
  1788. end.