cgcpu.pas 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_load_reg_ref')));
  1114. end;
  1115. procedure tcgz80.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;
  1116. const Ref : treference;reg : tregister);
  1117. var
  1118. href : treference;
  1119. i : integer;
  1120. begin
  1121. href:=Ref;
  1122. { ensure, href.base contains a valid register if there is any register used }
  1123. if href.base=NR_NO then
  1124. begin
  1125. href.base:=href.index;
  1126. href.index:=NR_NO;
  1127. end;
  1128. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1129. internalerror(2011021307);
  1130. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  1131. internalerror(2020040804);
  1132. if (tosize=fromsize) or (fromsize in [OS_8,OS_16,OS_32]) then
  1133. begin
  1134. getcpuregister(list,NR_A);
  1135. for i:=1 to tcgsize2size[fromsize] do
  1136. begin
  1137. list.concat(taicpu.op_reg_ref(A_LD,NR_A,href));
  1138. a_load_reg_reg(list,OS_8,OS_8,NR_A,reg);
  1139. if i<>tcgsize2size[fromsize] then
  1140. inc(href.offset);
  1141. if i<>tcgsize2size[tosize] then
  1142. reg:=GetNextReg(reg);
  1143. end;
  1144. ungetcpuregister(list,NR_A);
  1145. for i:=tcgsize2size[fromsize]+1 to tcgsize2size[tosize] do
  1146. begin
  1147. list.concat(taicpu.op_reg_const(A_LD,reg,0));
  1148. if i<>tcgsize2size[tosize] then
  1149. reg:=GetNextReg(reg);
  1150. end;
  1151. end
  1152. else
  1153. begin
  1154. getcpuregister(list,NR_A);
  1155. for i:=1 to tcgsize2size[fromsize] do
  1156. begin
  1157. list.concat(taicpu.op_reg_ref(A_LD,NR_A,href));
  1158. a_load_reg_reg(list,OS_8,OS_8,NR_A,reg);
  1159. if i<>tcgsize2size[fromsize] then
  1160. inc(href.offset);
  1161. if i<>tcgsize2size[tosize] then
  1162. reg:=GetNextReg(reg);
  1163. end;
  1164. list.concat(taicpu.op_none(A_RLA));
  1165. list.concat(taicpu.op_reg_reg(A_SBC,NR_A,NR_A));
  1166. for i:=tcgsize2size[fromsize]+1 to tcgsize2size[tosize] do
  1167. begin
  1168. emit_mov(list,reg,NR_A);
  1169. if i<>tcgsize2size[tosize] then
  1170. reg:=GetNextReg(reg);
  1171. end;
  1172. ungetcpuregister(list,NR_A);
  1173. end;
  1174. end;
  1175. procedure tcgz80.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1176. var
  1177. conv_done: boolean;
  1178. tmpreg : tregister;
  1179. i : integer;
  1180. begin
  1181. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1182. internalerror(2011021310);
  1183. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  1184. internalerror(2020040803);
  1185. if (tosize=fromsize) or (fromsize in [OS_8,OS_16,OS_32]) then
  1186. begin
  1187. if reg1<>reg2 then
  1188. for i:=1 to tcgsize2size[fromsize] do
  1189. begin
  1190. emit_mov(list,reg2,reg1);
  1191. if i<>tcgsize2size[fromsize] then
  1192. reg1:=GetNextReg(reg1);
  1193. if i<>tcgsize2size[tosize] then
  1194. reg2:=GetNextReg(reg2);
  1195. end
  1196. else
  1197. for i:=1 to tcgsize2size[fromsize] do
  1198. if i<>tcgsize2size[tosize] then
  1199. reg2:=GetNextReg(reg2);
  1200. for i:=tcgsize2size[fromsize]+1 to tcgsize2size[tosize] do
  1201. begin
  1202. list.Concat(taicpu.op_reg_const(A_LD,reg2,0));
  1203. if i<>tcgsize2size[tosize] then
  1204. reg2:=GetNextReg(reg2);
  1205. end
  1206. end
  1207. else
  1208. begin
  1209. if reg1<>reg2 then
  1210. for i:=1 to tcgsize2size[fromsize]-1 do
  1211. begin
  1212. emit_mov(list,reg2,reg1);
  1213. reg1:=GetNextReg(reg1);
  1214. reg2:=GetNextReg(reg2);
  1215. end
  1216. else
  1217. for i:=1 to tcgsize2size[fromsize]-1 do
  1218. reg2:=GetNextReg(reg2);
  1219. emit_mov(list,reg2,reg1);
  1220. getcpuregister(list,NR_A);
  1221. emit_mov(list,NR_A,reg2);
  1222. reg2:=GetNextReg(reg2);
  1223. list.concat(taicpu.op_none(A_RLA));
  1224. list.concat(taicpu.op_reg_reg(A_SBC,NR_A,NR_A));
  1225. for i:=tcgsize2size[fromsize]+1 to tcgsize2size[tosize] do
  1226. begin
  1227. emit_mov(list,reg2,NR_A);
  1228. if i<>tcgsize2size[tosize] then
  1229. reg2:=GetNextReg(reg2);
  1230. end;
  1231. ungetcpuregister(list,NR_A);
  1232. end;
  1233. end;
  1234. procedure tcgz80.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1235. begin
  1236. internalerror(2012010702);
  1237. end;
  1238. procedure tcgz80.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1239. begin
  1240. internalerror(2012010703);
  1241. end;
  1242. procedure tcgz80.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1243. begin
  1244. internalerror(2012010704);
  1245. end;
  1246. { comparison operations }
  1247. procedure tcgz80.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;
  1248. cmp_op : topcmp;a : tcgint;reg : tregister;l : tasmlabel);
  1249. var
  1250. swapped : boolean;
  1251. tmpreg : tregister;
  1252. i : byte;
  1253. begin
  1254. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_const_reg_label')));
  1255. //if a=0 then
  1256. // begin
  1257. // swapped:=false;
  1258. // { swap parameters? }
  1259. // case cmp_op of
  1260. // OC_GT:
  1261. // begin
  1262. // swapped:=true;
  1263. // cmp_op:=OC_LT;
  1264. // end;
  1265. // OC_LTE:
  1266. // begin
  1267. // swapped:=true;
  1268. // cmp_op:=OC_GTE;
  1269. // end;
  1270. // OC_BE:
  1271. // begin
  1272. // swapped:=true;
  1273. // cmp_op:=OC_AE;
  1274. // end;
  1275. // OC_A:
  1276. // begin
  1277. // swapped:=true;
  1278. // cmp_op:=OC_B;
  1279. // end;
  1280. // end;
  1281. //
  1282. // if swapped then
  1283. // list.concat(taicpu.op_reg_reg(A_CP,NR_R1,reg))
  1284. // else
  1285. // list.concat(taicpu.op_reg_reg(A_CP,reg,NR_R1));
  1286. //
  1287. // for i:=2 to tcgsize2size[size] do
  1288. // begin
  1289. // reg:=GetNextReg(reg);
  1290. // if swapped then
  1291. // list.concat(taicpu.op_reg_reg(A_CPC,NR_R1,reg))
  1292. // else
  1293. // list.concat(taicpu.op_reg_reg(A_CPC,reg,NR_R1));
  1294. // end;
  1295. //
  1296. // a_jmp_cond(list,cmp_op,l);
  1297. // end
  1298. //else
  1299. // inherited a_cmp_const_reg_label(list,size,cmp_op,a,reg,l);
  1300. end;
  1301. procedure tcgz80.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;
  1302. cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1303. var
  1304. swapped : boolean;
  1305. tmpreg : tregister;
  1306. i : byte;
  1307. begin
  1308. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_cmp_reg_reg_label')));
  1309. //swapped:=false;
  1310. //{ swap parameters? }
  1311. //case cmp_op of
  1312. // OC_GT:
  1313. // begin
  1314. // swapped:=true;
  1315. // cmp_op:=OC_LT;
  1316. // end;
  1317. // OC_LTE:
  1318. // begin
  1319. // swapped:=true;
  1320. // cmp_op:=OC_GTE;
  1321. // end;
  1322. // OC_BE:
  1323. // begin
  1324. // swapped:=true;
  1325. // cmp_op:=OC_AE;
  1326. // end;
  1327. // OC_A:
  1328. // begin
  1329. // swapped:=true;
  1330. // cmp_op:=OC_B;
  1331. // end;
  1332. //end;
  1333. //if swapped then
  1334. // begin
  1335. // tmpreg:=reg1;
  1336. // reg1:=reg2;
  1337. // reg2:=tmpreg;
  1338. // end;
  1339. //list.concat(taicpu.op_reg_reg(A_CP,reg2,reg1));
  1340. //
  1341. //for i:=2 to tcgsize2size[size] do
  1342. // begin
  1343. // reg1:=GetNextReg(reg1);
  1344. // reg2:=GetNextReg(reg2);
  1345. // list.concat(taicpu.op_reg_reg(A_CPC,reg2,reg1));
  1346. // end;
  1347. //
  1348. //a_jmp_cond(list,cmp_op,l);
  1349. end;
  1350. procedure tcgz80.a_jmp_name(list : TAsmList;const s : string);
  1351. var
  1352. ai : taicpu;
  1353. begin
  1354. ai:=taicpu.op_sym(A_JP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1355. ai.is_jmp:=true;
  1356. list.concat(ai);
  1357. end;
  1358. procedure tcgz80.a_jmp_always(list : TAsmList;l: tasmlabel);
  1359. var
  1360. ai : taicpu;
  1361. begin
  1362. ai:=taicpu.op_sym(A_JP,l);
  1363. ai.is_jmp:=true;
  1364. list.concat(ai);
  1365. end;
  1366. procedure tcgz80.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1367. var
  1368. ai : taicpu;
  1369. begin
  1370. ai:=taicpu.op_cond_sym(A_JP,flags_to_cond(f),l);
  1371. ai.is_jmp:=true;
  1372. list.concat(ai);
  1373. end;
  1374. procedure tcgz80.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1375. var
  1376. l : TAsmLabel;
  1377. tmpflags : TResFlags;
  1378. begin
  1379. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_flags2reg')));
  1380. current_asmdata.getjumplabel(l);
  1381. {
  1382. if flags_to_cond(f) then
  1383. begin
  1384. tmpflags:=f;
  1385. inverse_flags(tmpflags);
  1386. emit_mov(reg,NR_R1);
  1387. a_jmp_flags(list,tmpflags,l);
  1388. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1389. end
  1390. else
  1391. }
  1392. begin
  1393. //list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1394. //a_jmp_flags(list,f,l);
  1395. //emit_mov(list,reg,NR_R1);
  1396. end;
  1397. cg.a_label(list,l);
  1398. end;
  1399. procedure tcgz80.g_stackpointer_alloc(list: TAsmList; localsize: longint);
  1400. begin
  1401. if localsize>0 then
  1402. begin
  1403. list.Concat(taicpu.op_reg_const(A_LD,NR_HL,-localsize));
  1404. list.Concat(taicpu.op_reg_reg(A_ADD,NR_HL,NR_SP));
  1405. list.Concat(taicpu.op_reg_reg(A_LD,NR_SP,NR_HL));
  1406. end;
  1407. end;
  1408. procedure tcgz80.a_adjust_sp(list : TAsmList; value : longint);
  1409. var
  1410. i : integer;
  1411. begin
  1412. //case value of
  1413. // 0:
  1414. // ;
  1415. // {-14..-1:
  1416. // begin
  1417. // if ((-value) mod 2)<>0 then
  1418. // list.concat(taicpu.op_reg(A_PUSH,NR_R0));
  1419. // for i:=1 to (-value) div 2 do
  1420. // list.concat(taicpu.op_const(A_RCALL,0));
  1421. // end;
  1422. // 1..7:
  1423. // begin
  1424. // for i:=1 to value do
  1425. // list.concat(taicpu.op_reg(A_POP,NR_R0));
  1426. // end;}
  1427. // else
  1428. // begin
  1429. // list.concat(taicpu.op_reg_const(A_SUBI,NR_R28,lo(word(-value))));
  1430. // list.concat(taicpu.op_reg_const(A_SBCI,NR_R29,hi(word(-value))));
  1431. // // get SREG
  1432. // list.concat(taicpu.op_reg_const(A_IN,NR_R0,NIO_SREG));
  1433. //
  1434. // // block interrupts
  1435. // list.concat(taicpu.op_none(A_CLI));
  1436. //
  1437. // // write high SP
  1438. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_HI,NR_R29));
  1439. //
  1440. // // release interrupts
  1441. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SREG,NR_R0));
  1442. //
  1443. // // write low SP
  1444. // list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_LO,NR_R28));
  1445. // end;
  1446. //end;
  1447. end;
  1448. procedure tcgz80.make_simple_ref(list: TAsmList; var ref: treference);
  1449. begin
  1450. end;
  1451. procedure tcgz80.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1452. var
  1453. regsize,stackmisalignment: longint;
  1454. begin
  1455. regsize:=0;
  1456. stackmisalignment:=0;
  1457. { save old framepointer }
  1458. if not nostackframe then
  1459. begin
  1460. { return address }
  1461. inc(stackmisalignment,2);
  1462. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1463. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1464. begin
  1465. { push <frame_pointer> }
  1466. inc(stackmisalignment,2);
  1467. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1468. list.concat(Taicpu.op_reg(A_PUSH,NR_FRAME_POINTER_REG));
  1469. { Return address and FP are both on stack }
  1470. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*2);
  1471. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*2));
  1472. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  1473. begin
  1474. list.concat(Taicpu.op_reg_const(A_LD,NR_FRAME_POINTER_REG,0));
  1475. list.concat(Taicpu.op_reg_reg(A_ADD,NR_FRAME_POINTER_REG,NR_STACK_POINTER_REG))
  1476. end
  1477. else
  1478. begin
  1479. internalerror(2020040301);
  1480. (*push_regs;
  1481. gen_load_frame_for_exceptfilter(list);
  1482. { Need only as much stack space as necessary to do the calls.
  1483. Exception filters don't have own local vars, and temps are 'mapped'
  1484. to the parent procedure.
  1485. maxpushedparasize is already aligned at least on x86_64. }
  1486. localsize:=current_procinfo.maxpushedparasize;*)
  1487. end;
  1488. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1489. end
  1490. else
  1491. begin
  1492. CGmessage(cg_d_stackframe_omited);
  1493. end;
  1494. { allocate stackframe space }
  1495. if (localsize<>0) or
  1496. ((target_info.stackalign>sizeof(pint)) and
  1497. (stackmisalignment <> 0) and
  1498. ((pi_do_call in current_procinfo.flags) or
  1499. (po_assembler in current_procinfo.procdef.procoptions))) then
  1500. begin
  1501. if target_info.stackalign>sizeof(pint) then
  1502. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  1503. g_stackpointer_alloc(list,localsize);
  1504. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1505. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  1506. current_procinfo.final_localsize:=localsize;
  1507. end
  1508. end;
  1509. end;
  1510. procedure tcgz80.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1511. var
  1512. regs : tcpuregisterset;
  1513. reg : TSuperRegister;
  1514. LocalSize : longint;
  1515. begin
  1516. { every byte counts for Z80, so if a subroutine is marked as non-returning, we do
  1517. not generate any exit code, so we really trust the noreturn directive
  1518. }
  1519. if po_noreturn in current_procinfo.procdef.procoptions then
  1520. exit;
  1521. { remove stackframe }
  1522. if not nostackframe then
  1523. begin
  1524. stacksize:=current_procinfo.calc_stackframe_size;
  1525. if (target_info.stackalign>4) and
  1526. ((stacksize <> 0) or
  1527. (pi_do_call in current_procinfo.flags) or
  1528. { can't detect if a call in this case -> use nostackframe }
  1529. { if you (think you) know what you are doing }
  1530. (po_assembler in current_procinfo.procdef.procoptions)) then
  1531. stacksize := align(stacksize+sizeof(aint),target_info.stackalign) - sizeof(aint);
  1532. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1533. begin
  1534. internalerror(2020040302);
  1535. {if (stacksize<>0) then
  1536. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,stacksize,current_procinfo.framepointer);}
  1537. end
  1538. else
  1539. begin
  1540. list.Concat(taicpu.op_reg_reg(A_LD,NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1541. list.Concat(taicpu.op_reg(A_POP,NR_FRAME_POINTER_REG));
  1542. end;
  1543. list.concat(tai_regalloc.dealloc(current_procinfo.framepointer,nil));
  1544. end;
  1545. list.concat(taicpu.op_none(A_RET));
  1546. end;
  1547. procedure tcgz80.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1548. var
  1549. tmpref : treference;
  1550. begin
  1551. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_loadaddr_ref_reg')));
  1552. // if ref.addressmode<>AM_UNCHANGED then
  1553. // internalerror(2011021701);
  1554. //
  1555. //if assigned(ref.symbol) or (ref.offset<>0) then
  1556. // begin
  1557. // reference_reset(tmpref,0,[]);
  1558. // tmpref.symbol:=ref.symbol;
  1559. // tmpref.offset:=ref.offset;
  1560. //
  1561. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1562. // tmpref.refaddr:=addr_lo8_gs
  1563. // else
  1564. // tmpref.refaddr:=addr_lo8;
  1565. // list.concat(taicpu.op_reg_ref(A_LDI,r,tmpref));
  1566. //
  1567. // if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1568. // tmpref.refaddr:=addr_hi8_gs
  1569. // else
  1570. // tmpref.refaddr:=addr_hi8;
  1571. // list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(r),tmpref));
  1572. //
  1573. // if (ref.base<>NR_NO) then
  1574. // begin
  1575. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.base));
  1576. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.base)));
  1577. // end;
  1578. // if (ref.index<>NR_NO) then
  1579. // begin
  1580. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1581. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1582. // end;
  1583. // end
  1584. //else if (ref.base<>NR_NO)then
  1585. // begin
  1586. // emit_mov(list,r,ref.base);
  1587. // emit_mov(list,GetNextReg(r),GetNextReg(ref.base));
  1588. // if (ref.index<>NR_NO) then
  1589. // begin
  1590. // list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  1591. // list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  1592. // end;
  1593. // end
  1594. //else if (ref.index<>NR_NO) then
  1595. // begin
  1596. // emit_mov(list,r,ref.index);
  1597. // emit_mov(list,GetNextReg(r),GetNextReg(ref.index));
  1598. // end;
  1599. end;
  1600. procedure tcgz80.fixref(list : TAsmList;var ref : treference);
  1601. begin
  1602. internalerror(2011021320);
  1603. end;
  1604. procedure tcgz80.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1605. var
  1606. paraloc1,paraloc2,paraloc3 : TCGPara;
  1607. pd : tprocdef;
  1608. begin
  1609. pd:=search_system_proc('MOVE');
  1610. paraloc1.init;
  1611. paraloc2.init;
  1612. paraloc3.init;
  1613. {$warning TODO: implement!!!}
  1614. //paramanager.getintparaloc(list,pd,1,paraloc1);
  1615. //paramanager.getintparaloc(list,pd,2,paraloc2);
  1616. //paramanager.getintparaloc(list,pd,3,paraloc3);
  1617. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1618. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1619. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1620. paramanager.freecgpara(list,paraloc3);
  1621. paramanager.freecgpara(list,paraloc2);
  1622. paramanager.freecgpara(list,paraloc1);
  1623. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1624. a_call_name_static(list,'FPC_MOVE');
  1625. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1626. paraloc3.done;
  1627. paraloc2.done;
  1628. paraloc1.done;
  1629. end;
  1630. procedure tcgz80.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1631. var
  1632. countreg,tmpreg : tregister;
  1633. srcref,dstref : treference;
  1634. copysize,countregsize : tcgsize;
  1635. l : TAsmLabel;
  1636. i : longint;
  1637. SrcQuickRef, DestQuickRef : Boolean;
  1638. begin
  1639. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_concatcopy')));
  1640. //if len>16 then
  1641. // begin
  1642. // current_asmdata.getjumplabel(l);
  1643. //
  1644. // reference_reset(srcref,source.alignment,source.volatility);
  1645. // reference_reset(dstref,dest.alignment,source.volatility);
  1646. // srcref.base:=NR_R30;
  1647. // srcref.addressmode:=AM_POSTINCREMENT;
  1648. // dstref.base:=NR_R26;
  1649. // dstref.addressmode:=AM_POSTINCREMENT;
  1650. //
  1651. // copysize:=OS_8;
  1652. // if len<256 then
  1653. // countregsize:=OS_8
  1654. // else if len<65536 then
  1655. // countregsize:=OS_16
  1656. // else
  1657. // internalerror(2011022007);
  1658. // countreg:=getintregister(list,countregsize);
  1659. // a_load_const_reg(list,countregsize,len,countreg);
  1660. // a_loadaddr_ref_reg(list,source,NR_R30);
  1661. //
  1662. // { only base or index register in dest? }
  1663. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1664. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1665. // begin
  1666. // if dest.base<>NR_NO then
  1667. // tmpreg:=dest.base
  1668. // else if dest.index<>NR_NO then
  1669. // tmpreg:=dest.index
  1670. // else
  1671. // internalerror(2016112001);
  1672. // end
  1673. // else
  1674. // begin
  1675. // tmpreg:=getaddressregister(list);
  1676. // a_loadaddr_ref_reg(list,dest,tmpreg);
  1677. // end;
  1678. //
  1679. // { X is used for spilling code so we can load it
  1680. // only by a push/pop sequence, this can be
  1681. // optimized later on by the peephole optimizer
  1682. // }
  1683. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1684. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1685. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1686. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1687. // cg.a_label(list,l);
  1688. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1689. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1690. // list.concat(taicpu.op_reg(A_DEC,countreg));
  1691. // a_jmp_flags(list,F_NE,l);
  1692. // // keep registers alive
  1693. // list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1694. // end
  1695. //else
  1696. // begin
  1697. // SrcQuickRef:=false;
  1698. // DestQuickRef:=false;
  1699. // if not((source.addressmode=AM_UNCHANGED) and
  1700. // (source.symbol=nil) and
  1701. // ((source.base=NR_R28) or
  1702. // (source.base=NR_R30)) and
  1703. // (source.Index=NR_NO) and
  1704. // (source.Offset in [0..64-len])) and
  1705. // not((source.Base=NR_NO) and (source.Index=NR_NO)) then
  1706. // srcref:=normalize_ref(list,source,NR_R30)
  1707. // else
  1708. // begin
  1709. // SrcQuickRef:=true;
  1710. // srcref:=source;
  1711. // end;
  1712. //
  1713. // if not((dest.addressmode=AM_UNCHANGED) and
  1714. // (dest.symbol=nil) and
  1715. // ((dest.base=NR_R28) or
  1716. // (dest.base=NR_R30)) and
  1717. // (dest.Index=NR_No) and
  1718. // (dest.Offset in [0..64-len])) and
  1719. // not((dest.Base=NR_NO) and (dest.Index=NR_NO)) then
  1720. // begin
  1721. // if not(SrcQuickRef) then
  1722. // begin
  1723. // { only base or index register in dest? }
  1724. // if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  1725. // ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  1726. // begin
  1727. // if dest.base<>NR_NO then
  1728. // tmpreg:=dest.base
  1729. // else if dest.index<>NR_NO then
  1730. // tmpreg:=dest.index
  1731. // else
  1732. // internalerror(2016112002);
  1733. // end
  1734. // else
  1735. // tmpreg:=getaddressregister(list);
  1736. //
  1737. // dstref:=normalize_ref(list,dest,tmpreg);
  1738. //
  1739. // { X is used for spilling code so we can load it
  1740. // only by a push/pop sequence, this can be
  1741. // optimized later on by the peephole optimizer
  1742. // }
  1743. // list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  1744. // list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  1745. // list.concat(taicpu.op_reg(A_POP,NR_R27));
  1746. // list.concat(taicpu.op_reg(A_POP,NR_R26));
  1747. // dstref.base:=NR_R26;
  1748. // end
  1749. // else
  1750. // dstref:=normalize_ref(list,dest,NR_R30);
  1751. // end
  1752. // else
  1753. // begin
  1754. // DestQuickRef:=true;
  1755. // dstref:=dest;
  1756. // end;
  1757. //
  1758. // for i:=1 to len do
  1759. // begin
  1760. // if not(SrcQuickRef) and (i<len) then
  1761. // srcref.addressmode:=AM_POSTINCREMENT
  1762. // else
  1763. // srcref.addressmode:=AM_UNCHANGED;
  1764. //
  1765. // if not(DestQuickRef) and (i<len) then
  1766. // dstref.addressmode:=AM_POSTINCREMENT
  1767. // else
  1768. // dstref.addressmode:=AM_UNCHANGED;
  1769. //
  1770. // list.concat(taicpu.op_reg_ref(GetLoad(srcref),NR_R0,srcref));
  1771. // list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,NR_R0));
  1772. //
  1773. // if SrcQuickRef then
  1774. // inc(srcref.offset);
  1775. // if DestQuickRef then
  1776. // inc(dstref.offset);
  1777. // end;
  1778. // if not(SrcQuickRef) then
  1779. // begin
  1780. // ungetcpuregister(list,srcref.base);
  1781. // ungetcpuregister(list,GetNextReg(srcref.base));
  1782. // end;
  1783. // end;
  1784. end;
  1785. procedure tcgz80.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1786. var
  1787. hl : tasmlabel;
  1788. ai : taicpu;
  1789. cond : TAsmCond;
  1790. begin
  1791. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: g_overflowCheck')));
  1792. //if not(cs_check_overflow in current_settings.localswitches) then
  1793. // exit;
  1794. //current_asmdata.getjumplabel(hl);
  1795. //if not ((def.typ=pointerdef) or
  1796. // ((def.typ=orddef) and
  1797. // (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1798. // pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1799. // cond:=C_VC
  1800. //else
  1801. // cond:=C_CC;
  1802. //ai:=Taicpu.Op_Sym(A_BRxx,hl);
  1803. //ai.SetCondition(cond);
  1804. //ai.is_jmp:=true;
  1805. //list.concat(ai);
  1806. //
  1807. //a_call_name(list,'FPC_OVERFLOW',false);
  1808. //a_label(list,hl);
  1809. end;
  1810. procedure tcgz80.g_save_registers(list: TAsmList);
  1811. begin
  1812. { this is done by the entry code }
  1813. end;
  1814. procedure tcgz80.g_restore_registers(list: TAsmList);
  1815. begin
  1816. { this is done by the exit code }
  1817. end;
  1818. procedure tcgz80.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1819. var
  1820. ai1,ai2 : taicpu;
  1821. hl : TAsmLabel;
  1822. begin
  1823. list.Concat(tai_comment.Create(strpnew('WARNING! not implemented: a_jmp_cond')));
  1824. //ai1:=Taicpu.Op_sym(A_BRxx,l);
  1825. //ai1.is_jmp:=true;
  1826. //hl:=nil;
  1827. //case cond of
  1828. // OC_EQ:
  1829. // ai1.SetCondition(C_EQ);
  1830. // OC_GT:
  1831. // begin
  1832. // { emulate GT }
  1833. // current_asmdata.getjumplabel(hl);
  1834. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1835. // ai2.SetCondition(C_EQ);
  1836. // ai2.is_jmp:=true;
  1837. // list.concat(ai2);
  1838. //
  1839. // ai1.SetCondition(C_GE);
  1840. // end;
  1841. // OC_LT:
  1842. // ai1.SetCondition(C_LT);
  1843. // OC_GTE:
  1844. // ai1.SetCondition(C_GE);
  1845. // OC_LTE:
  1846. // begin
  1847. // { emulate LTE }
  1848. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1849. // ai2.SetCondition(C_EQ);
  1850. // ai2.is_jmp:=true;
  1851. // list.concat(ai2);
  1852. //
  1853. // ai1.SetCondition(C_LT);
  1854. // end;
  1855. // OC_NE:
  1856. // ai1.SetCondition(C_NE);
  1857. // OC_BE:
  1858. // begin
  1859. // { emulate BE }
  1860. // ai2:=Taicpu.Op_Sym(A_BRxx,l);
  1861. // ai2.SetCondition(C_EQ);
  1862. // ai2.is_jmp:=true;
  1863. // list.concat(ai2);
  1864. //
  1865. // ai1.SetCondition(C_LO);
  1866. // end;
  1867. // OC_B:
  1868. // ai1.SetCondition(C_LO);
  1869. // OC_AE:
  1870. // ai1.SetCondition(C_SH);
  1871. // OC_A:
  1872. // begin
  1873. // { emulate A (unsigned GT) }
  1874. // current_asmdata.getjumplabel(hl);
  1875. // ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  1876. // ai2.SetCondition(C_EQ);
  1877. // ai2.is_jmp:=true;
  1878. // list.concat(ai2);
  1879. //
  1880. // ai1.SetCondition(C_SH);
  1881. // end;
  1882. // else
  1883. // internalerror(2011082501);
  1884. //end;
  1885. //list.concat(ai1);
  1886. //if assigned(hl) then
  1887. // a_label(list,hl);
  1888. end;
  1889. procedure tcgz80.emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  1890. var
  1891. instr: taicpu;
  1892. begin
  1893. instr:=taicpu.op_reg_reg(A_LD,reg2,reg1);
  1894. list.Concat(instr);
  1895. { Notify the register allocator that we have written a move instruction so
  1896. it can try to eliminate it. }
  1897. add_move_instruction(instr);
  1898. end;
  1899. procedure tcg64fz80.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1900. begin
  1901. if not(size in [OS_S64,OS_64]) then
  1902. internalerror(2012102402);
  1903. tcgz80(cg).a_op_reg_reg_internal(list,Op,size,regsrc.reglo,regsrc.reghi,regdst.reglo,regdst.reghi);
  1904. end;
  1905. procedure tcg64fz80.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1906. begin
  1907. tcgz80(cg).a_op_const_reg_internal(list,Op,size,value,reg.reglo,reg.reghi);
  1908. end;
  1909. function GetByteLoc(const loc : tlocation; nr : byte) : tlocation;
  1910. var
  1911. i : Integer;
  1912. begin
  1913. Result:=loc;
  1914. Result.size:=OS_8;
  1915. case loc.loc of
  1916. LOC_REFERENCE,LOC_CREFERENCE:
  1917. inc(Result.reference.offset,nr);
  1918. LOC_REGISTER,LOC_CREGISTER:
  1919. begin
  1920. if nr>=4 then
  1921. Result.register:=Result.register64.reghi;
  1922. nr:=nr mod 4;
  1923. for i:=1 to nr do
  1924. Result.register:=GetNextReg(Result.register);
  1925. end;
  1926. LOC_CONSTANT:
  1927. if loc.size in [OS_64,OS_S64] then
  1928. Result.value:=(Result.value64 shr (nr*8)) and $ff
  1929. else
  1930. Result.value:=(Result.value shr (nr*8)) and $ff;
  1931. else
  1932. Internalerror(2019020902);
  1933. end;
  1934. end;
  1935. procedure create_codegen;
  1936. begin
  1937. cg:=tcgz80.create;
  1938. cg64:=tcg64fz80.create;
  1939. end;
  1940. end.