cgcpu.pas 80 KB

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