cgcpu.pas 79 KB

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