cgcpu.pas 72 KB

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