cgcpu.pas 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  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 AVR
  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. { tcgavr }
  29. tcgavr = 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. function GetHigh(const r : TRegister) : TRegister;inline;
  36. function GetOffsetReg(const r: TRegister;ofs : shortint): TRegister;override;
  37. function GetOffsetReg64(const r,rhi: TRegister;ofs : shortint): TRegister;override;
  38. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  39. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  40. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  41. procedure a_load_reg_cgpara(list : TAsmList; size : tcgsize;r : tregister; const cgpara : tcgpara);override;
  42. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  43. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  44. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  45. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src, dst : TRegister); override;
  46. procedure a_op_const_reg_reg(list : TAsmList;op : TOpCg;size : tcgsize; a : tcgint;src,dst : tregister); override;
  47. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  48. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  49. { move instructions }
  50. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  51. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  52. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  53. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  54. { fpu move instructions }
  55. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  56. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  57. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  58. { comparison operations }
  59. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  60. l : tasmlabel);override;
  61. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  62. procedure a_jmp_name(list : TAsmList;const s : string); override;
  63. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  64. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  65. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  66. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  67. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  68. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  69. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  70. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  71. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  72. procedure g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; ovloc: tlocation); override;
  73. procedure g_save_registers(list : TAsmList);override;
  74. procedure g_restore_registers(list : TAsmList);override;
  75. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  76. procedure fixref(list : TAsmList;var ref : treference);
  77. function normalize_ref(list : TAsmList;ref : treference;
  78. tmpreg : tregister) : treference;
  79. procedure emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  80. procedure a_adjust_sp(list: TAsmList; value: longint);
  81. function GetLoad(const ref : treference) : tasmop;
  82. function GetStore(const ref: treference): tasmop;
  83. procedure gen_multiply(list: TAsmList; op: topcg; size: TCgSize; src2, src1, dst: tregister; check_overflow: boolean; var ovloc: tlocation);
  84. private
  85. procedure a_op_const_reg_reg_internal(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, srchi, dst, dsthi: tregister);
  86. protected
  87. procedure a_op_reg_reg_internal(list: TAsmList; Op: TOpCG; size: TCGSize; src, srchi, dst, dsthi: TRegister);
  88. procedure a_op_const_reg_internal(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg, reghi: TRegister);
  89. procedure maybegetcpuregister(list : tasmlist; reg : tregister);
  90. function addr_is_io_register(const addr: integer): boolean;
  91. end;
  92. tcg64favr = class(tcg64f32)
  93. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  94. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  95. procedure a_op64_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; value: int64;src,dst: tregister64);override;
  96. end;
  97. procedure create_codegen;
  98. const
  99. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,
  100. A_NONE,A_MULS,A_MUL,A_NEG,A_COM,A_OR,
  101. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_ROL,A_ROR);
  102. implementation
  103. uses
  104. globals,verbose,systems,cutils,
  105. fmodule,
  106. symconst,symsym,symtable,
  107. tgobj,rgobj,
  108. procinfo,cpupi,
  109. paramgr;
  110. procedure tcgavr.init_register_allocators;
  111. begin
  112. inherited init_register_allocators;
  113. if CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype] then
  114. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  115. [RS_R18,RS_R19,RS_R20,RS_R21,RS_R22,RS_R23,RS_R24,RS_R25],first_int_imreg,[])
  116. else
  117. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  118. [RS_R18,RS_R19,RS_R20,RS_R21,RS_R22,RS_R23,RS_R24,RS_R25,
  119. RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,
  120. RS_R10,RS_R11,RS_R12,RS_R13,RS_R14,RS_R15,RS_R16,RS_R17],first_int_imreg,[]);
  121. end;
  122. procedure tcgavr.done_register_allocators;
  123. begin
  124. rg[R_INTREGISTER].free;
  125. // rg[R_ADDRESSREGISTER].free;
  126. inherited done_register_allocators;
  127. end;
  128. function tcgavr.getaddressregister(list: TAsmList): TRegister;
  129. begin
  130. Result:=getintregister(list,OS_ADDR);
  131. end;
  132. function tcgavr.GetHigh(const r : TRegister) : TRegister;
  133. begin
  134. result:=GetNextReg(r);
  135. end;
  136. function tcgavr.GetOffsetReg(const r: TRegister;ofs : shortint): TRegister;
  137. begin
  138. result:=TRegister(longint(r)+ofs);
  139. end;
  140. function tcgavr.GetOffsetReg64(const r,rhi: TRegister;ofs : shortint): TRegister;
  141. begin
  142. if ofs>3 then
  143. result:=TRegister(longint(rhi)+ofs-4)
  144. else
  145. result:=TRegister(longint(r)+ofs);
  146. end;
  147. procedure tcgavr.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  148. procedure load_para_loc(r : TRegister;paraloc : PCGParaLocation);
  149. var
  150. ref : treference;
  151. begin
  152. paramanager.allocparaloc(list,paraloc);
  153. case paraloc^.loc of
  154. LOC_REGISTER,LOC_CREGISTER:
  155. a_load_reg_reg(list,paraloc^.size,paraloc^.size,r,paraloc^.register);
  156. LOC_REFERENCE,LOC_CREFERENCE:
  157. begin
  158. reference_reset_base(ref,paraloc^.reference.index,paraloc^.reference.offset,ctempposinvalid,2,[]);
  159. if ref.base<>NR_STACK_POINTER_REG then
  160. Internalerror(2020011801);
  161. { as AVR allows no stack indirect addressing, everything else than a push makes no sense }
  162. list.concat(taicpu.op_reg(A_PUSH,r));
  163. end;
  164. else
  165. internalerror(2002071004);
  166. end;
  167. end;
  168. var
  169. i, i2 : longint;
  170. hp : PCGParaLocation;
  171. begin
  172. if not(tcgsize2size[cgpara.Size] in [1..4]) then
  173. internalerror(2014011101);
  174. hp:=cgpara.location;
  175. i:=0;
  176. while i<tcgsize2size[cgpara.Size] do
  177. begin
  178. if not(assigned(hp)) then
  179. internalerror(2014011102);
  180. inc(i, tcgsize2size[hp^.Size]);
  181. if hp^.Loc=LOC_REGISTER then
  182. begin
  183. load_para_loc(r,hp);
  184. hp:=hp^.Next;
  185. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  186. if i<tcgsize2size[cgpara.Size] then
  187. r:=GetNextReg(r);
  188. end
  189. else
  190. begin
  191. load_para_loc(r,hp);
  192. if i<tcgsize2size[cgpara.Size] then
  193. for i2:=1 to tcgsize2size[hp^.Size] do
  194. r:=GetNextReg(r);
  195. hp:=hp^.Next;
  196. end;
  197. end;
  198. if assigned(hp) then
  199. internalerror(2014011103);
  200. end;
  201. procedure tcgavr.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  202. var
  203. i,j : longint;
  204. hp : PCGParaLocation;
  205. ref: treference;
  206. tmpreg: TRegister;
  207. begin
  208. if not(tcgsize2size[paraloc.Size] in [1..4]) then
  209. internalerror(2014011101);
  210. hp:=paraloc.location;
  211. i:=1;
  212. while i<=tcgsize2size[paraloc.Size] do
  213. begin
  214. if not(assigned(hp)) then
  215. internalerror(2014011105);
  216. paramanager.allocparaloc(list,hp);
  217. case hp^.loc of
  218. LOC_REGISTER,LOC_CREGISTER:
  219. begin
  220. if (tcgsize2size[hp^.size]<>1) or
  221. (hp^.shiftval<>0) then
  222. internalerror(2015041101);
  223. a_load_const_reg(list,hp^.size,(a shr (8*(i-1))) and $ff,hp^.register);
  224. inc(i,tcgsize2size[hp^.size]);
  225. hp:=hp^.Next;
  226. end;
  227. LOC_REFERENCE,LOC_CREFERENCE:
  228. begin
  229. for j:=1 to tcgsize2size[hp^.size] do
  230. begin
  231. tmpreg:=getintregister(list,OS_8);
  232. a_load_const_reg(list,OS_8,(a shr (8*(i-1+j-1))) and $ff,tmpreg);
  233. { as AVR allows no stack indirect addressing, everything else than a push makes no sense }
  234. list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  235. end;
  236. inc(i,tcgsize2size[hp^.size]);
  237. hp:=hp^.Next;
  238. end;
  239. else
  240. internalerror(2002071004);
  241. end;
  242. end;
  243. end;
  244. procedure tcgavr.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  245. var
  246. tmpref, ref: treference;
  247. location: pcgparalocation;
  248. sizeleft: tcgint;
  249. i: Integer;
  250. tmpreg: TRegister;
  251. begin
  252. location := paraloc.location;
  253. tmpref := r;
  254. sizeleft := paraloc.intsize;
  255. while assigned(location) do
  256. begin
  257. paramanager.allocparaloc(list,location);
  258. case location^.loc of
  259. LOC_REGISTER,LOC_CREGISTER:
  260. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  261. LOC_REFERENCE:
  262. begin
  263. ref:=tmpref;
  264. for i:=1 to sizeleft do
  265. begin
  266. tmpreg:=getintregister(list,OS_8);
  267. a_load_ref_reg(list,OS_8,OS_8,tmpref,tmpreg);
  268. { as AVR allows no stack indirect addressing, everything else than a push makes no sense }
  269. list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  270. inc(tmpref.offset);
  271. end;
  272. end;
  273. LOC_VOID:
  274. begin
  275. // nothing to do
  276. end;
  277. else
  278. internalerror(2002081103);
  279. end;
  280. inc(tmpref.offset,tcgsize2size[location^.size]);
  281. dec(sizeleft,tcgsize2size[location^.size]);
  282. location := location^.next;
  283. end;
  284. end;
  285. procedure tcgavr.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  286. var
  287. tmpreg: tregister;
  288. begin
  289. tmpreg:=getaddressregister(list);
  290. a_loadaddr_ref_reg(list,r,tmpreg);
  291. a_load_reg_cgpara(list,OS_ADDR,tmpreg,paraloc);
  292. end;
  293. procedure tcgavr.a_call_name(list : TAsmList;const s : string; weak: boolean);
  294. var
  295. sym: TAsmSymbol;
  296. begin
  297. if weak then
  298. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)
  299. else
  300. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  301. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  302. list.concat(taicpu.op_sym(A_CALL,sym))
  303. else
  304. list.concat(taicpu.op_sym(A_RCALL,sym));
  305. include(current_procinfo.flags,pi_do_call);
  306. end;
  307. procedure tcgavr.a_call_reg(list : TAsmList;reg: tregister);
  308. begin
  309. a_reg_alloc(list,NR_ZLO);
  310. emit_mov(list,NR_ZLO,reg);
  311. a_reg_alloc(list,NR_ZHI);
  312. emit_mov(list,NR_ZHI,GetHigh(reg));
  313. list.concat(taicpu.op_none(A_ICALL));
  314. a_reg_dealloc(list,NR_ZHI);
  315. a_reg_dealloc(list,NR_ZLO);
  316. include(current_procinfo.flags,pi_do_call);
  317. end;
  318. procedure tcgavr.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  319. begin
  320. if not(size in [OS_S8,OS_8,OS_S16,OS_16,OS_S32,OS_32]) then
  321. internalerror(2012102403);
  322. a_op_const_reg_internal(list,Op,size,a,reg,NR_NO);
  323. end;
  324. procedure tcgavr.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src, dst : TRegister);
  325. begin
  326. if not(size in [OS_S8,OS_8,OS_S16,OS_16,OS_S32,OS_32]) then
  327. internalerror(2012102401);
  328. a_op_reg_reg_internal(list,Op,size,src,NR_NO,dst,NR_NO);
  329. end;
  330. procedure tcgavr.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  331. begin
  332. a_op_const_reg_reg_internal(list,op,size,a,src,NR_NO,dst,NR_NO);
  333. end;
  334. procedure tcgavr.a_op_const_reg_reg_internal(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src,srchi,dst,dsthi: tregister);
  335. var
  336. tmpSrc, tmpDst, countreg: TRegister;
  337. b, b2, i, j: byte;
  338. s1, s2, t1: integer;
  339. l1: TAsmLabel;
  340. oldexecutionweight: LongInt;
  341. begin
  342. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_16,OS_S16]) and (a in [2,4,8]) then
  343. begin
  344. emit_mov(list,dst,src);
  345. emit_mov(list,GetNextReg(dst),GetNextReg(src));
  346. a:=a shr 1;
  347. while a>0 do
  348. begin
  349. list.concat(taicpu.op_reg(A_LSL,dst));
  350. list.concat(taicpu.op_reg(A_ROL,GetNextReg(dst)));
  351. a:=a shr 1;
  352. end;
  353. end
  354. else if (op in [OP_SHL,OP_SHR]) and
  355. { a=0 get eliminated later by tcg.optimize_op_const }
  356. (a>0) then
  357. begin
  358. { number of bytes to shift }
  359. b:=a div 8;
  360. { Ensure that b is never larger than base type }
  361. if b>tcgsize2size[size] then
  362. begin
  363. b:=tcgsize2size[size];
  364. b2:=0;
  365. end
  366. else
  367. b2:=a mod 8;
  368. if b < tcgsize2size[size] then
  369. { copy from src to dst accounting for shift offset }
  370. for i:=0 to (tcgsize2size[size]-b-1) do
  371. if op=OP_SHL then
  372. a_load_reg_reg(list,OS_8,OS_8,
  373. GetOffsetReg64(src,srchi,i),
  374. GetOffsetReg64(dst,dsthi,i+b))
  375. else
  376. a_load_reg_reg(list,OS_8,OS_8,
  377. GetOffsetReg64(src,srchi,i+b),
  378. GetOffsetReg64(dst,dsthi,i));
  379. { remaining bit shifts }
  380. if b2 > 0 then
  381. begin
  382. { Cost of loop }
  383. s1:=3+tcgsize2size[size]-b;
  384. t1:=b2*(tcgsize2size[size]-b+3);
  385. { Cost of loop unrolling,t2=s2 }
  386. s2:=b2*(tcgsize2size[size]-b);
  387. if ((cs_opt_size in current_settings.optimizerswitches) and (s1<s2)) or
  388. (((s2-s1)-t1/s2)>0) then
  389. begin
  390. { Shift non-moved bytes in loop }
  391. current_asmdata.getjumplabel(l1);
  392. countreg:=getintregister(list,OS_8);
  393. a_load_const_reg(list,OS_8,b2,countreg);
  394. cg.a_label(list,l1);
  395. oldexecutionweight:=executionweight;
  396. executionweight:=executionweight*b2;
  397. if op=OP_SHL then
  398. list.concat(taicpu.op_reg(A_LSL,GetOffsetReg64(dst,dsthi,b)))
  399. else
  400. list.concat(taicpu.op_reg(A_LSR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1-b)));
  401. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  402. begin
  403. for i:=2+b to tcgsize2size[size] do
  404. if op=OP_SHL then
  405. list.concat(taicpu.op_reg(A_ROL,GetOffsetReg64(dst,dsthi,i-1)))
  406. else
  407. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i)));
  408. end;
  409. list.concat(taicpu.op_reg(A_DEC,countreg));
  410. a_jmp_flags(list,F_NE,l1);
  411. executionweight:=oldexecutionweight;
  412. { keep registers alive }
  413. a_reg_sync(list,countreg);
  414. end
  415. else
  416. begin
  417. { Unroll shift loop over non-moved bytes }
  418. for j:=1 to b2 do
  419. begin
  420. if op=OP_SHL then
  421. list.concat(taicpu.op_reg(A_LSL,
  422. GetOffsetReg64(dst,dsthi,b)))
  423. else
  424. list.concat(taicpu.op_reg(A_LSR,
  425. GetOffsetReg64(dst,dsthi,tcgsize2size[size]-b-1)));
  426. if not(size in [OS_8,OS_S8]) then
  427. for i:=2 to tcgsize2size[size]-b do
  428. if op=OP_SHL then
  429. list.concat(taicpu.op_reg(A_ROL,
  430. GetOffsetReg64(dst,dsthi,b+i-1)))
  431. else
  432. list.concat(taicpu.op_reg(A_ROR,
  433. GetOffsetReg64(dst,dsthi,tcgsize2size[size]-b-i)));
  434. end;
  435. end;
  436. end;
  437. { fill skipped destination registers with 0
  438. Do last,then optimizer can optimize register moves }
  439. for i:=1 to b do
  440. if op=OP_SHL then
  441. emit_mov(list,GetOffsetReg64(dst,dsthi,i-1),GetDefaultZeroReg)
  442. else
  443. emit_mov(list,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i),GetDefaultZeroReg);
  444. end
  445. else
  446. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  447. end;
  448. procedure tcgavr.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  449. var
  450. tmpreg: TRegister;
  451. begin
  452. if (op in [OP_MUL,OP_IMUL]) and
  453. setflags then
  454. begin
  455. tmpreg:=getintregister(list,size);
  456. a_load_const_reg(list,size,a,tmpreg);
  457. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  458. end
  459. else
  460. begin
  461. inherited a_op_const_reg_reg_checkoverflow(list, op, size, a, src, dst, setflags, ovloc);
  462. ovloc.loc:=LOC_FLAGS;
  463. end;
  464. end;
  465. procedure tcgavr.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  466. begin
  467. if (op in [OP_MUL,OP_IMUL]) and
  468. setflags then
  469. gen_multiply(list,op,size,src1,src2,dst,setflags,ovloc)
  470. else
  471. begin
  472. inherited a_op_reg_reg_reg_checkoverflow(list, op, size, src1, src2, dst, setflags, ovloc);
  473. ovloc.loc:=LOC_FLAGS;
  474. end;
  475. end;
  476. procedure tcgavr.a_op_reg_reg_internal(list : TAsmList; Op: TOpCG; size: TCGSize; src, srchi, dst, dsthi: TRegister);
  477. var
  478. countreg,
  479. tmpreg: tregister;
  480. i : integer;
  481. instr : taicpu;
  482. paraloc1,paraloc2 : TCGPara;
  483. l1,l2 : tasmlabel;
  484. pd : tprocdef;
  485. hovloc: tlocation;
  486. { NextRegDst* is sometimes called before the register usage and sometimes afterwards }
  487. procedure NextSrcDstPreInc;
  488. begin
  489. if i=5 then
  490. begin
  491. dst:=dsthi;
  492. src:=srchi;
  493. end
  494. else
  495. begin
  496. dst:=GetNextReg(dst);
  497. src:=GetNextReg(src);
  498. end;
  499. end;
  500. procedure NextSrcDstPostInc;
  501. begin
  502. if i=4 then
  503. begin
  504. dst:=dsthi;
  505. src:=srchi;
  506. end
  507. else
  508. begin
  509. dst:=GetNextReg(dst);
  510. src:=GetNextReg(src);
  511. end;
  512. end;
  513. { iterates TmpReg through all registers of dst }
  514. procedure NextTmp;
  515. begin
  516. if i=4 then
  517. tmpreg:=dsthi
  518. else
  519. tmpreg:=GetNextReg(tmpreg);
  520. end;
  521. begin
  522. case op of
  523. OP_ADD:
  524. begin
  525. list.concat(taicpu.op_reg_reg(A_ADD,dst,src));
  526. for i:=2 to tcgsize2size[size] do
  527. begin
  528. NextSrcDstPreInc;
  529. list.concat(taicpu.op_reg_reg(A_ADC,dst,src));
  530. end;
  531. end;
  532. OP_SUB:
  533. begin
  534. list.concat(taicpu.op_reg_reg(A_SUB,dst,src));
  535. for i:=2 to tcgsize2size[size] do
  536. begin
  537. NextSrcDstPreInc;
  538. list.concat(taicpu.op_reg_reg(A_SBC,dst,src));
  539. end;
  540. end;
  541. OP_NEG:
  542. begin
  543. if src<>dst then
  544. begin
  545. if size in [OS_S64,OS_64] then
  546. begin
  547. a_load_reg_reg(list,OS_32,OS_32,src,dst);
  548. a_load_reg_reg(list,OS_32,OS_32,srchi,dsthi);
  549. end
  550. else
  551. a_load_reg_reg(list,size,size,src,dst);
  552. end;
  553. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  554. begin
  555. tmpreg:=GetNextReg(dst);
  556. for i:=2 to tcgsize2size[size] do
  557. begin
  558. list.concat(taicpu.op_reg(A_COM,tmpreg));
  559. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  560. if i<tcgsize2size[size] then
  561. NextTmp;
  562. end;
  563. list.concat(taicpu.op_reg(A_NEG,dst));
  564. tmpreg:=GetNextReg(dst);
  565. for i:=2 to tcgsize2size[size] do
  566. begin
  567. list.concat(taicpu.op_reg_const(A_SBCI,tmpreg,-1));
  568. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  569. if i<tcgsize2size[size] then
  570. NextTmp;
  571. end;
  572. end
  573. else if size in [OS_S8,OS_8] then
  574. list.concat(taicpu.op_reg(A_NEG,dst))
  575. else
  576. Internalerror(2018030401);
  577. end;
  578. OP_NOT:
  579. begin
  580. for i:=1 to tcgsize2size[size] do
  581. begin
  582. if src<>dst then
  583. a_load_reg_reg(list,OS_8,OS_8,src,dst);
  584. list.concat(taicpu.op_reg(A_COM,dst));
  585. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  586. if i<tcgsize2size[size] then
  587. NextSrcDstPostInc;
  588. end;
  589. end;
  590. OP_MUL,OP_IMUL:
  591. begin
  592. tmpreg:=dst;
  593. if size in [OS_16,OS_S16] then
  594. begin
  595. tmpreg:=getintregister(list,size);
  596. a_load_reg_reg(list,size,size,dst,tmpreg);
  597. end;
  598. gen_multiply(list,op,size,src,tmpreg,dst,false,hovloc);
  599. end;
  600. OP_DIV,OP_IDIV:
  601. { special stuff, needs separate handling inside code }
  602. { generator }
  603. internalerror(2011022001);
  604. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  605. begin
  606. current_asmdata.getjumplabel(l1);
  607. current_asmdata.getjumplabel(l2);
  608. countreg:=getintregister(list,OS_8);
  609. a_load_reg_reg(list,size,OS_8,src,countreg);
  610. list.concat(taicpu.op_reg(A_TST,countreg));
  611. a_jmp_flags(list,F_EQ,l2);
  612. cg.a_label(list,l1);
  613. case op of
  614. OP_SHR:
  615. list.concat(taicpu.op_reg(A_LSR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  616. OP_SHL:
  617. list.concat(taicpu.op_reg(A_LSL,dst));
  618. OP_SAR:
  619. list.concat(taicpu.op_reg(A_ASR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  620. OP_ROR:
  621. begin
  622. { load carry? }
  623. if not(size in [OS_8,OS_S8]) then
  624. begin
  625. list.concat(taicpu.op_none(A_CLC));
  626. list.concat(taicpu.op_reg_const(A_SBRC,dst,0));
  627. list.concat(taicpu.op_none(A_SEC));
  628. end;
  629. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1)));
  630. end;
  631. OP_ROL:
  632. begin
  633. { load carry? }
  634. if not(size in [OS_8,OS_S8]) then
  635. begin
  636. list.concat(taicpu.op_none(A_CLC));
  637. list.concat(taicpu.op_reg_const(A_SBRC,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-1),7));
  638. list.concat(taicpu.op_none(A_SEC));
  639. end;
  640. list.concat(taicpu.op_reg(A_ROL,dst))
  641. end;
  642. else
  643. internalerror(2011030901);
  644. end;
  645. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  646. begin
  647. for i:=2 to tcgsize2size[size] do
  648. begin
  649. case op of
  650. OP_ROR,
  651. OP_SHR:
  652. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i)));
  653. OP_ROL,
  654. OP_SHL:
  655. list.concat(taicpu.op_reg(A_ROL,GetOffsetReg64(dst,dsthi,i-1)));
  656. OP_SAR:
  657. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(dst,dsthi,tcgsize2size[size]-i)));
  658. else
  659. internalerror(2011030902);
  660. end;
  661. end;
  662. end;
  663. list.concat(taicpu.op_reg(A_DEC,countreg));
  664. a_jmp_flags(list,F_NE,l1);
  665. { keep registers alive }
  666. a_reg_sync(list,countreg);
  667. cg.a_label(list,l2);
  668. end;
  669. OP_AND,OP_OR,OP_XOR:
  670. begin
  671. for i:=1 to tcgsize2size[size] do
  672. begin
  673. list.concat(taicpu.op_reg_reg(topcg2asmop[op],dst,src));
  674. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  675. if i<tcgsize2size[size] then
  676. NextSrcDstPostInc;
  677. end;
  678. end;
  679. else
  680. internalerror(2011022004);
  681. end;
  682. end;
  683. procedure tcgavr.a_op_const_reg_internal(list: TAsmList; Op: TOpCG;
  684. size: TCGSize; a: tcgint; reg, reghi: TRegister);
  685. var
  686. mask : qword;
  687. shift : byte;
  688. i,j : byte;
  689. tmpreg : tregister;
  690. tmpreg64 : tregister64;
  691. { NextReg* is sometimes called before the register usage and sometimes afterwards }
  692. procedure NextRegPreInc;
  693. begin
  694. if i=5 then
  695. reg:=reghi
  696. else
  697. reg:=GetNextReg(reg);
  698. end;
  699. procedure NextRegPostInc;
  700. begin
  701. if i=4 then
  702. reg:=reghi
  703. else
  704. reg:=GetNextReg(reg);
  705. end;
  706. var
  707. curvalue : byte;
  708. l1: TAsmLabel;
  709. begin
  710. optimize_op_const(size,op,a);
  711. mask:=$ff;
  712. shift:=0;
  713. case op of
  714. OP_NONE:
  715. begin
  716. { Opcode is optimized away }
  717. end;
  718. OP_MOVE:
  719. begin
  720. { Optimized, replaced with a simple load }
  721. a_load_const_reg(list,size,a,reg);
  722. end;
  723. OP_OR:
  724. begin
  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. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  730. if i<tcgsize2size[size] then
  731. NextRegPostInc;
  732. mask:=mask shl 8;
  733. inc(shift,8);
  734. end;
  735. end;
  736. OP_AND:
  737. begin
  738. for i:=1 to tcgsize2size[size] do
  739. begin
  740. if ((qword(a) and mask) shr shift)=0 then
  741. list.concat(taicpu.op_reg_reg(A_MOV,reg,GetDefaultZeroReg))
  742. else if ((qword(a) and mask) shr shift)<>$ff then
  743. begin
  744. getcpuregister(list,NR_R26);
  745. list.concat(taicpu.op_reg_const(A_LDI,NR_R26,(qword(a) and mask) shr shift));
  746. list.concat(taicpu.op_reg_reg(A_AND,reg,NR_R26));
  747. ungetcpuregister(list,NR_R26);
  748. end;
  749. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  750. if i<tcgsize2size[size] then
  751. NextRegPostInc;
  752. mask:=mask shl 8;
  753. inc(shift,8);
  754. end;
  755. end;
  756. OP_SUB:
  757. begin
  758. if ((a and mask)=1) and (tcgsize2size[size]=1) then
  759. list.concat(taicpu.op_reg(A_DEC,reg))
  760. else
  761. begin
  762. getcpuregister(list,NR_R26);
  763. list.concat(taicpu.op_reg_const(A_LDI,NR_R26,a and mask));
  764. list.concat(taicpu.op_reg_reg(A_SUB,reg,NR_R26));
  765. ungetcpuregister(list,NR_R26);
  766. end;
  767. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  768. begin
  769. for i:=2 to tcgsize2size[size] do
  770. begin
  771. NextRegPreInc;
  772. mask:=mask shl 8;
  773. inc(shift,8);
  774. curvalue:=(qword(a) and mask) shr shift;
  775. { decrease pressure on upper half of registers by using SBC ...,R1 instead
  776. of SBCI ...,0 }
  777. if curvalue=0 then
  778. list.concat(taicpu.op_reg_reg(A_SBC,reg,GetDefaultZeroReg))
  779. else
  780. list.concat(taicpu.op_reg_const(A_SBCI,reg,curvalue));
  781. end;
  782. end;
  783. end;
  784. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  785. begin
  786. if (op=OP_SAR) and (a>=(tcgsize2size[size]*8-1)) then
  787. begin
  788. current_asmdata.getjumplabel(l1);
  789. list.concat(taicpu.op_reg(A_TST,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  790. a_load_const_reg(list,OS_8,0,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1));
  791. a_jmp_flags(list,F_PL,l1);
  792. list.concat(taicpu.op_reg(A_DEC,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  793. cg.a_label(list,l1);
  794. for i:=2 to tcgsize2size[size] do
  795. a_load_reg_reg(list,OS_8,OS_8,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1),GetOffsetReg64(reg,reghi,tcgsize2size[size]-i));
  796. end
  797. else if (op=OP_SHR) and (a=(tcgsize2size[size]*8-1)) then
  798. begin
  799. current_asmdata.getjumplabel(l1);
  800. list.concat(taicpu.op_reg(A_TST,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  801. a_load_const_reg(list,OS_8,0,GetOffsetReg64(reg,reghi,0));
  802. a_jmp_flags(list,F_PL,l1);
  803. list.concat(taicpu.op_reg(A_INC,GetOffsetReg64(reg,reghi,0)));
  804. cg.a_label(list,l1);
  805. for i:=1 to tcgsize2size[size]-1 do
  806. a_load_const_reg(list,OS_8,0,GetOffsetReg64(reg,reghi,i));
  807. end
  808. else if a*tcgsize2size[size]<=8 then
  809. begin
  810. for j:=1 to a do
  811. begin
  812. case op of
  813. OP_SHR:
  814. list.concat(taicpu.op_reg(A_LSR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  815. OP_SHL:
  816. list.concat(taicpu.op_reg(A_LSL,reg));
  817. OP_SAR:
  818. list.concat(taicpu.op_reg(A_ASR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  819. OP_ROR:
  820. begin
  821. { load carry? }
  822. if not(size in [OS_8,OS_S8]) then
  823. begin
  824. list.concat(taicpu.op_none(A_CLC));
  825. list.concat(taicpu.op_reg_const(A_SBRC,reg,0));
  826. list.concat(taicpu.op_none(A_SEC));
  827. end;
  828. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1)));
  829. end;
  830. OP_ROL:
  831. begin
  832. { load carry? }
  833. if not(size in [OS_8,OS_S8]) then
  834. begin
  835. list.concat(taicpu.op_none(A_CLC));
  836. list.concat(taicpu.op_reg_const(A_SBRC,GetOffsetReg64(reg,reghi,tcgsize2size[size]-1),7));
  837. list.concat(taicpu.op_none(A_SEC));
  838. end;
  839. list.concat(taicpu.op_reg(A_ROL,reg))
  840. end;
  841. else
  842. internalerror(2011030901);
  843. end;
  844. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  845. begin
  846. for i:=2 to tcgsize2size[size] do
  847. begin
  848. case op of
  849. OP_ROR,
  850. OP_SHR:
  851. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-i)));
  852. OP_ROL,
  853. OP_SHL:
  854. list.concat(taicpu.op_reg(A_ROL,GetOffsetReg64(reg,reghi,i-1)));
  855. OP_SAR:
  856. list.concat(taicpu.op_reg(A_ROR,GetOffsetReg64(reg,reghi,tcgsize2size[size]-i)));
  857. else
  858. internalerror(2011030902);
  859. end;
  860. end;
  861. end;
  862. end;
  863. end
  864. else
  865. begin
  866. tmpreg:=getintregister(list,size);
  867. a_load_const_reg(list,size,a,tmpreg);
  868. a_op_reg_reg(list,op,size,tmpreg,reg);
  869. end;
  870. end;
  871. OP_ADD:
  872. begin
  873. curvalue:=a and mask;
  874. if curvalue=0 then
  875. list.concat(taicpu.op_reg_reg(A_ADD,reg,GetDefaultZeroReg))
  876. else if (curvalue=1) and (tcgsize2size[size]=1) then
  877. list.concat(taicpu.op_reg(A_INC,reg))
  878. else
  879. begin
  880. tmpreg:=getintregister(list,OS_8);
  881. a_load_const_reg(list,OS_8,curvalue,tmpreg);
  882. list.concat(taicpu.op_reg_reg(A_ADD,reg,tmpreg));
  883. end;
  884. if size in [OS_S16,OS_16,OS_S32,OS_32,OS_S64,OS_64] then
  885. begin
  886. for i:=2 to tcgsize2size[size] do
  887. begin
  888. NextRegPreInc;
  889. mask:=mask shl 8;
  890. inc(shift,8);
  891. curvalue:=(qword(a) and mask) shr shift;
  892. { decrease pressure on upper half of registers by using ADC ...,R1 instead
  893. of ADD ...,0 }
  894. if curvalue=0 then
  895. list.concat(taicpu.op_reg_reg(A_ADC,reg,GetDefaultZeroReg))
  896. else
  897. begin
  898. tmpreg:=getintregister(list,OS_8);
  899. a_load_const_reg(list,OS_8,curvalue,tmpreg);
  900. list.concat(taicpu.op_reg_reg(A_ADC,reg,tmpreg));
  901. end;
  902. end;
  903. end;
  904. end;
  905. else
  906. begin
  907. if size in [OS_64,OS_S64] then
  908. begin
  909. tmpreg64.reglo:=getintregister(list,OS_32);
  910. tmpreg64.reghi:=getintregister(list,OS_32);
  911. cg64.a_load64_const_reg(list,a,tmpreg64);
  912. cg64.a_op64_reg_reg(list,op,size,tmpreg64,joinreg64(reg,reghi));
  913. end
  914. else
  915. begin
  916. {$if 0}
  917. { code not working yet }
  918. if (op=OP_SAR) and (a=31) and (size in [OS_32,OS_S32]) then
  919. begin
  920. tmpreg:=reg;
  921. for i:=1 to 4 do
  922. begin
  923. list.concat(taicpu.op_reg_reg(A_MOV,tmpreg,GetDefaultZeroReg));
  924. tmpreg:=GetNextReg(tmpreg);
  925. end;
  926. end
  927. else
  928. {$endif}
  929. begin
  930. tmpreg:=getintregister(list,size);
  931. a_load_const_reg(list,size,a,tmpreg);
  932. a_op_reg_reg(list,op,size,tmpreg,reg);
  933. end;
  934. end;
  935. end;
  936. end;
  937. end;
  938. procedure tcgavr.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  939. var
  940. mask : qword;
  941. shift : byte;
  942. i : byte;
  943. begin
  944. mask:=$ff;
  945. shift:=0;
  946. for i:=1 to tcgsize2size[size] do
  947. begin
  948. if ((qword(a) and mask) shr shift)=0 then
  949. emit_mov(list,reg,GetDefaultZeroReg)
  950. else
  951. begin
  952. getcpuregister(list,NR_R26);
  953. list.concat(taicpu.op_reg_const(A_LDI,NR_R26,(qword(a) and mask) shr shift));
  954. a_load_reg_reg(list,OS_8,OS_8,NR_R26,reg);
  955. ungetcpuregister(list,NR_R26);
  956. end;
  957. mask:=mask shl 8;
  958. inc(shift,8);
  959. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  960. if i<tcgsize2size[size] then
  961. reg:=GetNextReg(reg);
  962. end;
  963. end;
  964. procedure tcgavr.maybegetcpuregister(list:tasmlist;reg : tregister);
  965. begin
  966. { allocate the register only, if a cpu register is passed }
  967. if getsupreg(reg)<first_int_imreg then
  968. getcpuregister(list,reg);
  969. end;
  970. { Returns true if dataspace address falls in I/O register range }
  971. function tcgavr.addr_is_io_register(const addr: integer): boolean;
  972. begin
  973. result := (not(current_settings.cputype in [cpu_avrxmega3,cpu_avrtiny]) and (addr>31)) or
  974. ((current_settings.cputype in [cpu_avrxmega3,cpu_avrtiny]) and (addr>=0)) and
  975. (addr<cpuinfo.embedded_controllers[current_settings.controllertype].srambase);
  976. end;
  977. function tcgavr.normalize_ref(list:TAsmList;ref: treference;tmpreg : tregister) : treference;
  978. var
  979. tmpref : treference;
  980. l : tasmlabel;
  981. begin
  982. Result:=ref;
  983. if ref.addressmode<>AM_UNCHANGED then
  984. internalerror(2011021701);
  985. { Be sure to have a base register }
  986. if (ref.base=NR_NO) then
  987. begin
  988. ref.base:=ref.index;
  989. ref.index:=NR_NO;
  990. end;
  991. { can we take advantage of adiw/sbiw? }
  992. if (current_settings.cputype>=cpu_avr2) and not(assigned(ref.symbol)) and (ref.offset<>0) and (ref.offset>=-63) and (ref.offset<=63) and
  993. ((tmpreg=NR_R24) or (tmpreg=NR_R26) or (tmpreg=NR_R28) or (tmpreg=NR_R30)) and (ref.base<>NR_NO) then
  994. begin
  995. maybegetcpuregister(list,tmpreg);
  996. emit_mov(list,tmpreg,ref.base);
  997. maybegetcpuregister(list,GetNextReg(tmpreg));
  998. emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  999. if ref.index<>NR_NO then
  1000. begin
  1001. list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  1002. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  1003. end;
  1004. if ref.offset>0 then
  1005. list.concat(taicpu.op_reg_const(A_ADIW,tmpreg,ref.offset))
  1006. else
  1007. list.concat(taicpu.op_reg_const(A_SBIW,tmpreg,-ref.offset));
  1008. ref.offset:=0;
  1009. ref.base:=tmpreg;
  1010. ref.index:=NR_NO;
  1011. end
  1012. else if assigned(ref.symbol) or (ref.offset<>0) then
  1013. begin
  1014. reference_reset(tmpref,0,[]);
  1015. tmpref.symbol:=ref.symbol;
  1016. tmpref.offset:=ref.offset;
  1017. if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1018. tmpref.refaddr:=addr_lo8_gs
  1019. else
  1020. tmpref.refaddr:=addr_lo8;
  1021. maybegetcpuregister(list,tmpreg);
  1022. list.concat(taicpu.op_reg_ref(A_LDI,tmpreg,tmpref));
  1023. if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  1024. tmpref.refaddr:=addr_hi8_gs
  1025. else
  1026. tmpref.refaddr:=addr_hi8;
  1027. maybegetcpuregister(list,GetNextReg(tmpreg));
  1028. list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(tmpreg),tmpref));
  1029. if (ref.base<>NR_NO) then
  1030. begin
  1031. list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.base));
  1032. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.base)));
  1033. end;
  1034. if (ref.index<>NR_NO) then
  1035. begin
  1036. list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  1037. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  1038. end;
  1039. ref.symbol:=nil;
  1040. ref.offset:=0;
  1041. ref.base:=tmpreg;
  1042. ref.index:=NR_NO;
  1043. end
  1044. else if (ref.base<>NR_NO) and (ref.index<>NR_NO) then
  1045. begin
  1046. maybegetcpuregister(list,tmpreg);
  1047. emit_mov(list,tmpreg,ref.base);
  1048. maybegetcpuregister(list,GetNextReg(tmpreg));
  1049. emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  1050. list.concat(taicpu.op_reg_reg(A_ADD,tmpreg,ref.index));
  1051. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(tmpreg),GetNextReg(ref.index)));
  1052. ref.base:=tmpreg;
  1053. ref.index:=NR_NO;
  1054. end
  1055. else if (ref.base<>NR_NO) then
  1056. begin
  1057. maybegetcpuregister(list,tmpreg);
  1058. emit_mov(list,tmpreg,ref.base);
  1059. maybegetcpuregister(list,GetNextReg(tmpreg));
  1060. emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.base));
  1061. ref.base:=tmpreg;
  1062. ref.index:=NR_NO;
  1063. end
  1064. else if (ref.index<>NR_NO) then
  1065. begin
  1066. maybegetcpuregister(list,tmpreg);
  1067. emit_mov(list,tmpreg,ref.index);
  1068. maybegetcpuregister(list,GetNextReg(tmpreg));
  1069. emit_mov(list,GetNextReg(tmpreg),GetNextReg(ref.index));
  1070. ref.base:=tmpreg;
  1071. ref.index:=NR_NO;
  1072. end
  1073. else
  1074. Internalerror(2020011901);
  1075. Result:=ref;
  1076. end;
  1077. procedure tcgavr.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  1078. var
  1079. href : treference;
  1080. conv_done: boolean;
  1081. tmpreg : tregister;
  1082. i : integer;
  1083. QuickRef,ungetcpuregister_z: Boolean;
  1084. begin
  1085. QuickRef:=false;
  1086. ungetcpuregister_z:=false;
  1087. href:=Ref;
  1088. { ensure, href.base contains a valid register if there is any register used }
  1089. if href.base=NR_NO then
  1090. begin
  1091. href.base:=href.index;
  1092. href.index:=NR_NO;
  1093. end;
  1094. { try to use std/sts }
  1095. if not((href.Base=NR_NO) and (href.Index=NR_NO)) then
  1096. begin
  1097. if not((href.addressmode=AM_UNCHANGED) and
  1098. (href.symbol=nil) and
  1099. (href.Index=NR_NO) and
  1100. (href.Offset in [0..64-tcgsize2size[fromsize]])) or
  1101. (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  1102. begin
  1103. href:=normalize_ref(list,href,NR_R30);
  1104. getcpuregister(list,NR_R30);
  1105. getcpuregister(list,NR_R31);
  1106. ungetcpuregister_z:=true;
  1107. end
  1108. else
  1109. begin
  1110. if (href.base<>NR_R28) and (href.base<>NR_R30) then
  1111. begin
  1112. getcpuregister(list,NR_R30);
  1113. emit_mov(list,NR_R30,href.base);
  1114. getcpuregister(list,NR_R31);
  1115. emit_mov(list,NR_R31,GetNextReg(href.base));
  1116. href.base:=NR_R30;
  1117. ungetcpuregister_z:=true;
  1118. end;
  1119. QuickRef:=true;
  1120. end;
  1121. end
  1122. else
  1123. QuickRef:=true;
  1124. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1125. internalerror(2011021307);
  1126. conv_done:=false;
  1127. if tosize<>fromsize then
  1128. begin
  1129. conv_done:=true;
  1130. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1131. fromsize:=tosize;
  1132. case fromsize of
  1133. OS_8:
  1134. begin
  1135. if not(QuickRef) and (tcgsize2size[tosize]>1) then
  1136. href.addressmode:=AM_POSTINCREMENT;
  1137. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1138. for i:=2 to tcgsize2size[tosize] do
  1139. begin
  1140. if QuickRef then
  1141. inc(href.offset);
  1142. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1143. href.addressmode:=AM_POSTINCREMENT
  1144. else
  1145. href.addressmode:=AM_UNCHANGED;
  1146. list.concat(taicpu.op_ref_reg(GetStore(href),href,GetDefaultZeroReg));
  1147. end;
  1148. end;
  1149. OS_S8:
  1150. begin
  1151. if not(QuickRef) and (tcgsize2size[tosize]>1) then
  1152. href.addressmode:=AM_POSTINCREMENT;
  1153. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1154. if tcgsize2size[tosize]>1 then
  1155. begin
  1156. tmpreg:=getintregister(list,OS_8);
  1157. emit_mov(list,tmpreg,GetDefaultZeroReg);
  1158. list.concat(taicpu.op_reg_const(A_SBRC,reg,7));
  1159. list.concat(taicpu.op_reg(A_COM,tmpreg));
  1160. for i:=2 to tcgsize2size[tosize] do
  1161. begin
  1162. if QuickRef then
  1163. inc(href.offset);
  1164. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1165. href.addressmode:=AM_POSTINCREMENT
  1166. else
  1167. href.addressmode:=AM_UNCHANGED;
  1168. list.concat(taicpu.op_ref_reg(GetStore(href),href,tmpreg));
  1169. end;
  1170. end;
  1171. end;
  1172. OS_16:
  1173. begin
  1174. if not(QuickRef) and (tcgsize2size[tosize]>1) then
  1175. href.addressmode:=AM_POSTINCREMENT;
  1176. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1177. if QuickRef then
  1178. inc(href.offset)
  1179. else if not(QuickRef) and (tcgsize2size[fromsize]>2) then
  1180. href.addressmode:=AM_POSTINCREMENT
  1181. else
  1182. href.addressmode:=AM_UNCHANGED;
  1183. reg:=GetNextReg(reg);
  1184. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1185. for i:=3 to tcgsize2size[tosize] do
  1186. begin
  1187. if QuickRef then
  1188. inc(href.offset);
  1189. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1190. href.addressmode:=AM_POSTINCREMENT
  1191. else
  1192. href.addressmode:=AM_UNCHANGED;
  1193. list.concat(taicpu.op_ref_reg(GetStore(href),href,GetDefaultZeroReg));
  1194. end;
  1195. end;
  1196. OS_S16:
  1197. begin
  1198. if not(QuickRef) and (tcgsize2size[tosize]>1) then
  1199. href.addressmode:=AM_POSTINCREMENT;
  1200. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1201. if QuickRef then
  1202. inc(href.offset)
  1203. else if not(QuickRef) and (tcgsize2size[fromsize]>2) then
  1204. href.addressmode:=AM_POSTINCREMENT
  1205. else
  1206. href.addressmode:=AM_UNCHANGED;
  1207. reg:=GetNextReg(reg);
  1208. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1209. if tcgsize2size[tosize]>2 then
  1210. begin
  1211. tmpreg:=getintregister(list,OS_8);
  1212. emit_mov(list,tmpreg,GetDefaultZeroReg);
  1213. list.concat(taicpu.op_reg_const(A_SBRC,reg,7));
  1214. list.concat(taicpu.op_reg(A_COM,tmpreg));
  1215. for i:=3 to tcgsize2size[tosize] do
  1216. begin
  1217. if QuickRef then
  1218. inc(href.offset);
  1219. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1220. href.addressmode:=AM_POSTINCREMENT
  1221. else
  1222. href.addressmode:=AM_UNCHANGED;
  1223. list.concat(taicpu.op_ref_reg(GetStore(href),href,tmpreg));
  1224. end;
  1225. end;
  1226. end;
  1227. else
  1228. conv_done:=false;
  1229. end;
  1230. end;
  1231. if not conv_done then
  1232. begin
  1233. { Write to 16 bit ioreg, first high byte then low byte
  1234. sequence required for 16 bit timer registers
  1235. See e.g. atmega328p manual para 15.3 Accessing 16 bit registers
  1236. Avrxmega3: write low byte first then high byte
  1237. See e.g. megaAVR-0 family data sheet 7.5.6 Accessing 16-bit registers }
  1238. if (current_settings.cputype <> cpu_avrxmega3) and
  1239. (fromsize in [OS_16, OS_S16]) and QuickRef and addr_is_io_register(href.offset) then
  1240. begin
  1241. tmpreg:=GetNextReg(reg);
  1242. href.addressmode:=AM_UNCHANGED;
  1243. inc(href.offset);
  1244. list.concat(taicpu.op_ref_reg(GetStore(href),href,tmpreg));
  1245. dec(href.offset);
  1246. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1247. end
  1248. else
  1249. begin
  1250. for i:=1 to tcgsize2size[fromsize] do
  1251. begin
  1252. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1253. href.addressmode:=AM_POSTINCREMENT
  1254. else
  1255. href.addressmode:=AM_UNCHANGED;
  1256. list.concat(taicpu.op_ref_reg(GetStore(href),href,reg));
  1257. if QuickRef then
  1258. inc(href.offset);
  1259. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  1260. if i<tcgsize2size[fromsize] then
  1261. reg:=GetNextReg(reg);
  1262. end;
  1263. end;
  1264. end;
  1265. if not(QuickRef) or ungetcpuregister_z then
  1266. begin
  1267. ungetcpuregister(list,href.base);
  1268. ungetcpuregister(list,GetNextReg(href.base));
  1269. end;
  1270. end;
  1271. procedure tcgavr.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;
  1272. const Ref : treference;reg : tregister);
  1273. var
  1274. href : treference;
  1275. conv_done: boolean;
  1276. tmpreg : tregister;
  1277. i : integer;
  1278. QuickRef,ungetcpuregister_z: boolean;
  1279. begin
  1280. QuickRef:=false;
  1281. ungetcpuregister_z:=false;
  1282. href:=Ref;
  1283. { ensure, href.base contains a valid register if there is any register used }
  1284. if href.base=NR_NO then
  1285. begin
  1286. href.base:=href.index;
  1287. href.index:=NR_NO;
  1288. end;
  1289. { try to use ldd/lds }
  1290. if not((href.Base=NR_NO) and (href.Index=NR_NO)) then
  1291. begin
  1292. if not((href.addressmode=AM_UNCHANGED) and
  1293. (href.symbol=nil) and
  1294. (href.Index=NR_NO) and
  1295. (href.Offset in [0..64-tcgsize2size[fromsize]])) or
  1296. (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  1297. begin
  1298. href:=normalize_ref(list,href,NR_R30);
  1299. getcpuregister(list,NR_R30);
  1300. getcpuregister(list,NR_R31);
  1301. ungetcpuregister_z:=true;
  1302. end
  1303. else
  1304. begin
  1305. if (href.base<>NR_R28) and (href.base<>NR_R30) then
  1306. begin
  1307. getcpuregister(list,NR_R30);
  1308. emit_mov(list,NR_R30,href.base);
  1309. getcpuregister(list,NR_R31);
  1310. emit_mov(list,NR_R31,GetNextReg(href.base));
  1311. href.base:=NR_R30;
  1312. ungetcpuregister_z:=true;
  1313. end;
  1314. QuickRef:=true;
  1315. end;
  1316. end
  1317. else
  1318. QuickRef:=true;
  1319. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1320. internalerror(2011021307);
  1321. conv_done:=false;
  1322. if tosize<>fromsize then
  1323. begin
  1324. conv_done:=true;
  1325. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1326. fromsize:=tosize;
  1327. case fromsize of
  1328. OS_8:
  1329. begin
  1330. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1331. for i:=2 to tcgsize2size[tosize] do
  1332. begin
  1333. reg:=GetNextReg(reg);
  1334. emit_mov(list,reg,GetDefaultZeroReg);
  1335. end;
  1336. end;
  1337. OS_S8:
  1338. begin
  1339. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1340. tmpreg:=reg;
  1341. if tcgsize2size[tosize]>1 then
  1342. begin
  1343. reg:=GetNextReg(reg);
  1344. emit_mov(list,reg,GetDefaultZeroReg);
  1345. list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  1346. list.concat(taicpu.op_reg(A_COM,reg));
  1347. tmpreg:=reg;
  1348. for i:=3 to tcgsize2size[tosize] do
  1349. begin
  1350. reg:=GetNextReg(reg);
  1351. emit_mov(list,reg,tmpreg);
  1352. end;
  1353. end;
  1354. end;
  1355. OS_16:
  1356. begin
  1357. if not(QuickRef) then
  1358. href.addressmode:=AM_POSTINCREMENT;
  1359. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1360. if QuickRef then
  1361. inc(href.offset);
  1362. href.addressmode:=AM_UNCHANGED;
  1363. reg:=GetNextReg(reg);
  1364. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1365. for i:=3 to tcgsize2size[tosize] do
  1366. begin
  1367. reg:=GetNextReg(reg);
  1368. emit_mov(list,reg,GetDefaultZeroReg);
  1369. end;
  1370. end;
  1371. OS_S16:
  1372. begin
  1373. if not(QuickRef) then
  1374. href.addressmode:=AM_POSTINCREMENT;
  1375. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1376. if QuickRef then
  1377. inc(href.offset);
  1378. href.addressmode:=AM_UNCHANGED;
  1379. reg:=GetNextReg(reg);
  1380. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1381. tmpreg:=reg;
  1382. reg:=GetNextReg(reg);
  1383. emit_mov(list,reg,GetDefaultZeroReg);
  1384. list.concat(taicpu.op_reg_const(A_SBRC,tmpreg,7));
  1385. list.concat(taicpu.op_reg(A_COM,reg));
  1386. tmpreg:=reg;
  1387. for i:=4 to tcgsize2size[tosize] do
  1388. begin
  1389. reg:=GetNextReg(reg);
  1390. emit_mov(list,reg,tmpreg);
  1391. end;
  1392. end;
  1393. else
  1394. conv_done:=false;
  1395. end;
  1396. end;
  1397. if not conv_done then
  1398. begin
  1399. for i:=1 to tcgsize2size[fromsize] do
  1400. begin
  1401. if not(QuickRef) and (i<tcgsize2size[fromsize]) then
  1402. href.addressmode:=AM_POSTINCREMENT
  1403. else
  1404. href.addressmode:=AM_UNCHANGED;
  1405. list.concat(taicpu.op_reg_ref(GetLoad(href),reg,href));
  1406. if QuickRef then
  1407. inc(href.offset);
  1408. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  1409. if i<tcgsize2size[fromsize] then
  1410. reg:=GetNextReg(reg);
  1411. end;
  1412. end;
  1413. if ungetcpuregister_z then
  1414. begin
  1415. ungetcpuregister(list,href.base);
  1416. ungetcpuregister(list,GetNextReg(href.base));
  1417. end;
  1418. end;
  1419. procedure tcgavr.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1420. var
  1421. conv_done: boolean;
  1422. tmpreg : tregister;
  1423. i : integer;
  1424. begin
  1425. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1426. internalerror(2011021310);
  1427. conv_done:=false;
  1428. if tosize<>fromsize then
  1429. begin
  1430. conv_done:=true;
  1431. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1432. fromsize:=tosize;
  1433. case fromsize of
  1434. OS_8:
  1435. begin
  1436. emit_mov(list,reg2,reg1);
  1437. for i:=2 to tcgsize2size[tosize] do
  1438. begin
  1439. reg2:=GetNextReg(reg2);
  1440. emit_mov(list,reg2,GetDefaultZeroReg);
  1441. end;
  1442. end;
  1443. OS_S8:
  1444. begin
  1445. emit_mov(list,reg2,reg1);
  1446. if tcgsize2size[tosize]>1 then
  1447. begin
  1448. reg2:=GetNextReg(reg2);
  1449. emit_mov(list,reg2,GetDefaultZeroReg);
  1450. list.concat(taicpu.op_reg_const(A_SBRC,reg1,7));
  1451. list.concat(taicpu.op_reg(A_COM,reg2));
  1452. tmpreg:=reg2;
  1453. for i:=3 to tcgsize2size[tosize] do
  1454. begin
  1455. reg2:=GetNextReg(reg2);
  1456. emit_mov(list,reg2,tmpreg);
  1457. end;
  1458. end;
  1459. end;
  1460. OS_16:
  1461. begin
  1462. emit_mov(list,reg2,reg1);
  1463. reg1:=GetNextReg(reg1);
  1464. reg2:=GetNextReg(reg2);
  1465. emit_mov(list,reg2,reg1);
  1466. for i:=3 to tcgsize2size[tosize] do
  1467. begin
  1468. reg2:=GetNextReg(reg2);
  1469. emit_mov(list,reg2,GetDefaultZeroReg);
  1470. end;
  1471. end;
  1472. OS_S16:
  1473. begin
  1474. emit_mov(list,reg2,reg1);
  1475. reg1:=GetNextReg(reg1);
  1476. reg2:=GetNextReg(reg2);
  1477. emit_mov(list,reg2,reg1);
  1478. if tcgsize2size[tosize]>2 then
  1479. begin
  1480. reg2:=GetNextReg(reg2);
  1481. emit_mov(list,reg2,GetDefaultZeroReg);
  1482. list.concat(taicpu.op_reg_const(A_SBRC,reg1,7));
  1483. list.concat(taicpu.op_reg(A_COM,reg2));
  1484. tmpreg:=reg2;
  1485. for i:=4 to tcgsize2size[tosize] do
  1486. begin
  1487. reg2:=GetNextReg(reg2);
  1488. emit_mov(list,reg2,tmpreg);
  1489. end;
  1490. end;
  1491. end;
  1492. else
  1493. conv_done:=false;
  1494. end;
  1495. end;
  1496. if not conv_done and (reg1<>reg2) then
  1497. begin
  1498. for i:=1 to tcgsize2size[fromsize] do
  1499. begin
  1500. emit_mov(list,reg2,reg1);
  1501. { check if we are not in the last iteration to avoid an internalerror in GetNextReg }
  1502. if i<tcgsize2size[fromsize] then
  1503. begin
  1504. reg1:=GetNextReg(reg1);
  1505. reg2:=GetNextReg(reg2);
  1506. end;
  1507. end;
  1508. end;
  1509. end;
  1510. procedure tcgavr.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1511. begin
  1512. internalerror(2012010702);
  1513. end;
  1514. procedure tcgavr.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1515. begin
  1516. internalerror(2012010703);
  1517. end;
  1518. procedure tcgavr.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1519. begin
  1520. internalerror(2012010704);
  1521. end;
  1522. { comparison operations }
  1523. procedure tcgavr.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;
  1524. cmp_op : topcmp;a : tcgint;reg : tregister;l : tasmlabel);
  1525. var
  1526. swapped , test_msb: boolean;
  1527. tmpreg : tregister;
  1528. i : byte;
  1529. begin
  1530. if a=0 then
  1531. begin
  1532. swapped:=false;
  1533. { swap parameters? }
  1534. case cmp_op of
  1535. OC_GT:
  1536. begin
  1537. swapped:=true;
  1538. cmp_op:=OC_LT;
  1539. end;
  1540. OC_LTE:
  1541. begin
  1542. swapped:=true;
  1543. cmp_op:=OC_GTE;
  1544. end;
  1545. OC_BE:
  1546. begin
  1547. swapped:=true;
  1548. cmp_op:=OC_AE;
  1549. end;
  1550. OC_A:
  1551. begin
  1552. swapped:=true;
  1553. cmp_op:=OC_B;
  1554. end;
  1555. end;
  1556. { If doing a signed test for x<0, we can simply test the sign bit
  1557. of the most significant byte }
  1558. if (cmp_op in [OC_LT,OC_GTE]) and
  1559. (not swapped) then
  1560. begin
  1561. for i:=2 to tcgsize2size[size] do
  1562. reg:=GetNextReg(reg);
  1563. list.concat(taicpu.op_reg_reg(A_CP,reg,GetDefaultZeroReg));
  1564. end
  1565. else
  1566. begin
  1567. if swapped then
  1568. list.concat(taicpu.op_reg_reg(A_CP,GetDefaultZeroReg,reg))
  1569. else
  1570. list.concat(taicpu.op_reg_reg(A_CP,reg,GetDefaultZeroReg));
  1571. for i:=2 to tcgsize2size[size] do
  1572. begin
  1573. reg:=GetNextReg(reg);
  1574. if swapped then
  1575. list.concat(taicpu.op_reg_reg(A_CPC,GetDefaultZeroReg,reg))
  1576. else
  1577. list.concat(taicpu.op_reg_reg(A_CPC,reg,GetDefaultZeroReg));
  1578. end;
  1579. end;
  1580. a_jmp_cond(list,cmp_op,l);
  1581. end
  1582. else
  1583. inherited a_cmp_const_reg_label(list,size,cmp_op,a,reg,l);
  1584. end;
  1585. procedure tcgavr.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;
  1586. cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1587. var
  1588. swapped : boolean;
  1589. tmpreg : tregister;
  1590. i : byte;
  1591. begin
  1592. swapped:=false;
  1593. { swap parameters? }
  1594. case cmp_op of
  1595. OC_GT:
  1596. begin
  1597. swapped:=true;
  1598. cmp_op:=OC_LT;
  1599. end;
  1600. OC_LTE:
  1601. begin
  1602. swapped:=true;
  1603. cmp_op:=OC_GTE;
  1604. end;
  1605. OC_BE:
  1606. begin
  1607. swapped:=true;
  1608. cmp_op:=OC_AE;
  1609. end;
  1610. OC_A:
  1611. begin
  1612. swapped:=true;
  1613. cmp_op:=OC_B;
  1614. end;
  1615. end;
  1616. if swapped then
  1617. begin
  1618. tmpreg:=reg1;
  1619. reg1:=reg2;
  1620. reg2:=tmpreg;
  1621. end;
  1622. list.concat(taicpu.op_reg_reg(A_CP,reg2,reg1));
  1623. for i:=2 to tcgsize2size[size] do
  1624. begin
  1625. reg1:=GetNextReg(reg1);
  1626. reg2:=GetNextReg(reg2);
  1627. list.concat(taicpu.op_reg_reg(A_CPC,reg2,reg1));
  1628. end;
  1629. a_jmp_cond(list,cmp_op,l);
  1630. end;
  1631. procedure tcgavr.a_jmp_name(list : TAsmList;const s : string);
  1632. var
  1633. ai : taicpu;
  1634. begin
  1635. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  1636. ai:=taicpu.op_sym(A_JMP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION))
  1637. else
  1638. ai:=taicpu.op_sym(A_RJMP,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1639. ai.is_jmp:=true;
  1640. list.concat(ai);
  1641. end;
  1642. procedure tcgavr.a_jmp_always(list : TAsmList;l: tasmlabel);
  1643. var
  1644. ai : taicpu;
  1645. begin
  1646. if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
  1647. ai:=taicpu.op_sym(A_JMP,l)
  1648. else
  1649. ai:=taicpu.op_sym(A_RJMP,l);
  1650. ai.is_jmp:=true;
  1651. list.concat(ai);
  1652. end;
  1653. procedure tcgavr.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1654. var
  1655. ai : taicpu;
  1656. begin
  1657. ai:=setcondition(taicpu.op_sym(A_BRxx,l),flags_to_cond(f));
  1658. ai.is_jmp:=true;
  1659. list.concat(ai);
  1660. end;
  1661. procedure tcgavr.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1662. var
  1663. l : TAsmLabel;
  1664. tmpflags : TResFlags;
  1665. i: Integer;
  1666. hreg: TRegister;
  1667. begin
  1668. current_asmdata.getjumplabel(l);
  1669. {
  1670. if flags_to_cond(f) then
  1671. begin
  1672. tmpflags:=f;
  1673. inverse_flags(tmpflags);
  1674. emit_mov(reg,GetDefaultZeroReg);
  1675. a_jmp_flags(list,tmpflags,l);
  1676. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1677. end
  1678. else
  1679. }
  1680. begin
  1681. list.concat(taicpu.op_reg_const(A_LDI,reg,1));
  1682. hreg:=reg;
  1683. for i:=2 to tcgsize2size[size] do
  1684. begin
  1685. hreg:=GetNextReg(hreg);
  1686. emit_mov(list,hreg,GetDefaultZeroReg);
  1687. end;
  1688. a_jmp_flags(list,f,l);
  1689. emit_mov(list,reg,GetDefaultZeroReg);
  1690. end;
  1691. cg.a_label(list,l);
  1692. end;
  1693. procedure tcgavr.a_adjust_sp(list : TAsmList; value : longint);
  1694. var
  1695. i : integer;
  1696. begin
  1697. case value of
  1698. 0:
  1699. ;
  1700. {-14..-1:
  1701. begin
  1702. if ((-value) mod 2)<>0 then
  1703. list.concat(taicpu.op_reg(A_PUSH,GetDefaultTmpReg));
  1704. for i:=1 to (-value) div 2 do
  1705. list.concat(taicpu.op_const(A_RCALL,0));
  1706. end;
  1707. 1..7:
  1708. begin
  1709. for i:=1 to value do
  1710. list.concat(taicpu.op_reg(A_POP,GetDefaultTmpReg));
  1711. end;}
  1712. else
  1713. begin
  1714. list.concat(taicpu.op_reg_const(A_SUBI,NR_R28,lo(word(-value))));
  1715. list.concat(taicpu.op_reg_const(A_SBCI,NR_R29,hi(word(-value))));
  1716. // get SREG
  1717. list.concat(taicpu.op_reg_const(A_IN,GetDefaultTmpReg,NIO_SREG));
  1718. // block interrupts
  1719. list.concat(taicpu.op_none(A_CLI));
  1720. // write high SP
  1721. list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_HI,NR_R29));
  1722. // release interrupts
  1723. list.concat(taicpu.op_const_reg(A_OUT,NIO_SREG,GetDefaultTmpReg));
  1724. // write low SP
  1725. list.concat(taicpu.op_const_reg(A_OUT,NIO_SP_LO,NR_R28));
  1726. end;
  1727. end;
  1728. end;
  1729. function tcgavr.GetLoad(const ref: treference) : tasmop;
  1730. begin
  1731. if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1732. result:=A_LDS
  1733. else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1734. result:=A_LDD
  1735. else
  1736. result:=A_LD;
  1737. end;
  1738. function tcgavr.GetStore(const ref: treference) : tasmop;
  1739. begin
  1740. if (ref.base=NR_NO) and (ref.index=NR_NO) then
  1741. result:=A_STS
  1742. else if (ref.base<>NR_NO) and (ref.offset<>0) then
  1743. result:=A_STD
  1744. else
  1745. result:=A_ST;
  1746. end;
  1747. procedure tcgavr.gen_multiply(list: TAsmList; op: topcg; size: TCgSize; src2, src1, dst: tregister; check_overflow: boolean; var ovloc: tlocation);
  1748. procedure perform_r1_check(overflow_label: TAsmLabel; other_reg: TRegister=NR_R1);
  1749. var
  1750. ai: taicpu;
  1751. begin
  1752. if check_overflow then
  1753. begin
  1754. list.concat(taicpu.op_reg_reg(A_OR,NR_R1,other_reg));
  1755. ai:=Taicpu.Op_Sym(A_BRxx,overflow_label);
  1756. ai.SetCondition(C_NE);
  1757. ai.is_jmp:=true;
  1758. list.concat(ai);
  1759. end;
  1760. end;
  1761. procedure perform_ovf_check(overflow_label: TAsmLabel);
  1762. var
  1763. ai: taicpu;
  1764. begin
  1765. if check_overflow then
  1766. begin
  1767. ai:=Taicpu.Op_Sym(A_BRxx,overflow_label);
  1768. ai.SetCondition(C_CS);
  1769. ai.is_jmp:=true;
  1770. list.concat(ai);
  1771. end;
  1772. end;
  1773. var
  1774. pd: tprocdef;
  1775. paraloc1, paraloc2: tcgpara;
  1776. ai: taicpu;
  1777. hl, no_overflow: TAsmLabel;
  1778. name: String;
  1779. begin
  1780. ovloc.loc:=LOC_VOID;
  1781. if size in [OS_8,OS_S8] then
  1782. begin
  1783. if (CPUAVR_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  1784. (op=OP_MUL) then
  1785. begin
  1786. cg.a_reg_alloc(list,NR_R0);
  1787. cg.a_reg_alloc(list,NR_R1);
  1788. list.concat(taicpu.op_reg_reg(topcg2asmop[op],src1,src2));
  1789. // Check overflow
  1790. if check_overflow then
  1791. begin
  1792. current_asmdata.getjumplabel(hl);
  1793. list.concat(taicpu.op_reg_reg(A_AND,NR_R1,NR_R1));
  1794. { Clear carry as it's not affected by any of the instructions }
  1795. list.concat(taicpu.op_none(A_CLC));
  1796. ai:=Taicpu.Op_Sym(A_BRxx,hl);
  1797. ai.SetCondition(C_EQ);
  1798. ai.is_jmp:=true;
  1799. list.concat(ai);
  1800. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1801. list.concat(taicpu.op_none(A_SEC));
  1802. a_label(list,hl);
  1803. ovloc.loc:=LOC_FLAGS;
  1804. end
  1805. else
  1806. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1807. cg.a_reg_dealloc(list,NR_R1);
  1808. list.concat(taicpu.op_reg_reg(A_MOV,dst,NR_R0));
  1809. cg.a_reg_dealloc(list,NR_R0);
  1810. end
  1811. else if (CPUAVR_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  1812. (op=OP_IMUL) then
  1813. begin
  1814. cg.a_reg_alloc(list,NR_R0);
  1815. cg.a_reg_alloc(list,NR_R1);
  1816. list.concat(taicpu.op_reg_reg(A_MULS,src1,src2));
  1817. list.concat(taicpu.op_reg_reg(A_MOV,dst,NR_R0));
  1818. // Check overflow
  1819. if check_overflow then
  1820. begin
  1821. current_asmdata.getjumplabel(no_overflow);
  1822. list.concat(taicpu.op_reg_const(A_SBRC,NR_R0,7));
  1823. list.concat(taicpu.op_reg(A_INC,NR_R1));
  1824. list.concat(taicpu.op_reg(A_TST,NR_R1));
  1825. ai:=Taicpu.Op_Sym(A_BRxx,no_overflow);
  1826. ai.SetCondition(C_EQ);
  1827. ai.is_jmp:=true;
  1828. list.concat(ai);
  1829. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1830. a_call_name(list,'FPC_OVERFLOW',false);
  1831. a_label(list,no_overflow);
  1832. ovloc.loc:=LOC_VOID;
  1833. end
  1834. else
  1835. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1836. cg.a_reg_dealloc(list,NR_R1);
  1837. cg.a_reg_dealloc(list,NR_R0);
  1838. end
  1839. else
  1840. begin
  1841. if size=OS_8 then
  1842. name:='fpc_mul_byte'
  1843. else
  1844. name:='fpc_mul_shortint';
  1845. if check_overflow then
  1846. name:=name+'_checkoverflow';
  1847. pd:=search_system_proc(name);
  1848. paraloc1.init;
  1849. paraloc2.init;
  1850. paramanager.getintparaloc(list,pd,1,paraloc1);
  1851. paramanager.getintparaloc(list,pd,2,paraloc2);
  1852. a_load_reg_cgpara(list,OS_8,src1,paraloc2);
  1853. a_load_reg_cgpara(list,OS_8,src2,paraloc1);
  1854. paramanager.freecgpara(list,paraloc2);
  1855. paramanager.freecgpara(list,paraloc1);
  1856. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1857. a_call_name(list,upper(name),false);
  1858. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1859. cg.a_reg_alloc(list,NR_R24);
  1860. cg.a_load_reg_reg(list,OS_8,OS_8,NR_R24,dst);
  1861. cg.a_reg_dealloc(list,NR_R24);
  1862. paraloc2.done;
  1863. paraloc1.done;
  1864. end;
  1865. end
  1866. else if size in [OS_16,OS_S16] then
  1867. begin
  1868. if (CPUAVR_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  1869. ((not check_overflow) or
  1870. (size=OS_16)) then
  1871. begin
  1872. if check_overflow then
  1873. begin
  1874. current_asmdata.getjumplabel(hl);
  1875. current_asmdata.getjumplabel(no_overflow);
  1876. end;
  1877. cg.a_reg_alloc(list,NR_R0);
  1878. cg.a_reg_alloc(list,NR_R1);
  1879. list.concat(taicpu.op_reg_reg(A_MUL,src2,src1));
  1880. emit_mov(list,dst,NR_R0);
  1881. emit_mov(list,GetNextReg(dst),NR_R1);
  1882. list.concat(taicpu.op_reg_reg(A_MUL,GetNextReg(src1),src2));
  1883. perform_r1_check(hl);
  1884. list.concat(taicpu.op_reg_reg(A_ADD,GetNextReg(dst),NR_R0));
  1885. perform_ovf_check(hl);
  1886. list.concat(taicpu.op_reg_reg(A_MUL,src1,GetNextReg(src2)));
  1887. perform_r1_check(hl);
  1888. list.concat(taicpu.op_reg_reg(A_ADD,GetNextReg(dst),NR_R0));
  1889. perform_ovf_check(hl);
  1890. if check_overflow then
  1891. begin
  1892. list.concat(taicpu.op_reg_reg(A_MUL,GetNextReg(src1),GetNextReg(src2)));
  1893. perform_r1_check(hl,NR_R0);
  1894. end;
  1895. cg.a_reg_dealloc(list,NR_R0);
  1896. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1897. if check_overflow then
  1898. begin
  1899. {
  1900. CLV/CLC
  1901. JMP no_overflow
  1902. .hl:
  1903. CLR R1
  1904. SEV/SEC
  1905. .no_overflow:
  1906. }
  1907. if op=OP_MUL then
  1908. list.concat(taicpu.op_none(A_CLC))
  1909. else
  1910. list.concat(taicpu.op_none(A_CLV));
  1911. a_jmp_always(list,no_overflow);
  1912. a_label(list,hl);
  1913. list.concat(taicpu.op_reg(A_CLR,NR_R1));
  1914. if op=OP_MUL then
  1915. list.concat(taicpu.op_none(A_SEC))
  1916. else
  1917. list.concat(taicpu.op_none(A_SEV));
  1918. a_label(list,no_overflow);
  1919. ovloc.loc:=LOC_FLAGS;
  1920. end;
  1921. cg.a_reg_dealloc(list,NR_R1);
  1922. end
  1923. else
  1924. begin
  1925. if size=OS_16 then
  1926. name:='fpc_mul_word'
  1927. else
  1928. name:='fpc_mul_integer';
  1929. if check_overflow then
  1930. name:=name+'_checkoverflow';
  1931. pd:=search_system_proc(name);
  1932. paraloc1.init;
  1933. paraloc2.init;
  1934. paramanager.getintparaloc(list,pd,1,paraloc1);
  1935. paramanager.getintparaloc(list,pd,2,paraloc2);
  1936. a_load_reg_cgpara(list,OS_16,src1,paraloc2);
  1937. a_load_reg_cgpara(list,OS_16,src2,paraloc1);
  1938. paramanager.freecgpara(list,paraloc2);
  1939. paramanager.freecgpara(list,paraloc1);
  1940. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1941. a_call_name(list,upper(name),false);
  1942. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1943. cg.a_reg_alloc(list,NR_R24);
  1944. cg.a_reg_alloc(list,NR_R25);
  1945. cg.a_load_reg_reg(list,OS_8,OS_8,NR_R24,dst);
  1946. cg.a_reg_dealloc(list,NR_R24);
  1947. cg.a_load_reg_reg(list,OS_8,OS_8,NR_R25,GetNextReg(dst));
  1948. cg.a_reg_dealloc(list,NR_R25);
  1949. paraloc2.done;
  1950. paraloc1.done;
  1951. end;
  1952. end
  1953. else
  1954. internalerror(2011022002);
  1955. end;
  1956. procedure tcgavr.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1957. var
  1958. regs : tcpuregisterset;
  1959. reg : tsuperregister;
  1960. begin
  1961. if current_procinfo.procdef.isempty then
  1962. exit;
  1963. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1964. (not nostackframe) then
  1965. begin
  1966. { check if the framepointer is actually used, this is done here because
  1967. we have to know the size of the locals (must be 0), avr does not know
  1968. an sp based stack }
  1969. if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  1970. (localsize=0) then
  1971. current_procinfo.framepointer:=NR_NO;
  1972. { save int registers,
  1973. but only if the procedure returns }
  1974. if not(po_noreturn in current_procinfo.procdef.procoptions) then
  1975. regs:=rg[R_INTREGISTER].used_in_proc
  1976. else
  1977. regs:=[];
  1978. { if the framepointer is potentially used, save it always because we need a proper stack frame,
  1979. even if the procedure never returns, the procedure could be e.g. a nested one accessing
  1980. an outer stackframe }
  1981. if current_procinfo.framepointer<>NR_NO then
  1982. regs:=regs+[RS_R28,RS_R29];
  1983. { we clear r1 }
  1984. include(regs,getsupreg(GetDefaultZeroReg));
  1985. regs:=regs+[getsupreg(GetDefaultTmpReg)];
  1986. if current_settings.cputype=cpu_avr1 then
  1987. message1(cg_w_interrupt_does_not_save_registers,current_procinfo.procdef.fullprocname(false))
  1988. else
  1989. begin
  1990. for reg:=RS_R31 downto RS_R0 do
  1991. if reg in regs then
  1992. list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  1993. { Save SREG }
  1994. cg.getcpuregister(list,GetDefaultTmpReg);
  1995. list.concat(taicpu.op_reg_const(A_IN, GetDefaultTmpReg, $3F));
  1996. list.concat(taicpu.op_reg(A_PUSH, GetDefaultTmpReg));
  1997. cg.ungetcpuregister(list,GetDefaultTmpReg);
  1998. end;
  1999. list.concat(taicpu.op_reg(A_CLR,GetDefaultZeroReg));
  2000. if current_procinfo.framepointer<>NR_NO then
  2001. begin
  2002. cg.getcpuregister(list,NR_R28);
  2003. list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  2004. cg.getcpuregister(list,NR_R29);
  2005. list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  2006. a_adjust_sp(list,-localsize);
  2007. end;
  2008. end
  2009. else if not(nostackframe) then
  2010. begin
  2011. { check if the framepointer is actually used, this is done here because
  2012. we have to know the size of the locals (must be 0), avr does not know
  2013. an sp based stack }
  2014. if not(current_procinfo.procdef.stack_tainting_parameter(calleeside)) and
  2015. (localsize=0) then
  2016. current_procinfo.framepointer:=NR_NO;
  2017. { save int registers,
  2018. but only if the procedure returns }
  2019. if not(po_noreturn in current_procinfo.procdef.procoptions) then
  2020. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)
  2021. else
  2022. regs:=[];
  2023. { if the framepointer is potentially used, save it always because we need a proper stack frame,
  2024. even if the procedure never returns, the procedure could be e.g. a nested one accessing
  2025. an outer stackframe }
  2026. if current_procinfo.framepointer<>NR_NO then
  2027. regs:=regs+[RS_R28,RS_R29];
  2028. for reg:=RS_R31 downto RS_R0 do
  2029. if reg in regs then
  2030. list.concat(taicpu.op_reg(A_PUSH,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  2031. if current_procinfo.framepointer<>NR_NO then
  2032. begin
  2033. cg.getcpuregister(list,NR_R28);
  2034. list.concat(taicpu.op_reg_const(A_IN,NR_R28,NIO_SP_LO));
  2035. cg.getcpuregister(list,NR_R29);
  2036. list.concat(taicpu.op_reg_const(A_IN,NR_R29,NIO_SP_HI));
  2037. a_adjust_sp(list,-localsize);
  2038. end;
  2039. end;
  2040. end;
  2041. procedure tcgavr.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  2042. var
  2043. regs : tcpuregisterset;
  2044. reg : TSuperRegister;
  2045. LocalSize : longint;
  2046. begin
  2047. { every byte counts for avr, so if a subroutine is marked as non-returning, we do
  2048. not generate any exit code, so we really trust the noreturn directive
  2049. }
  2050. if po_noreturn in current_procinfo.procdef.procoptions then
  2051. exit;
  2052. if po_interrupt in current_procinfo.procdef.procoptions then
  2053. begin
  2054. if not(current_procinfo.procdef.isempty) and
  2055. (not nostackframe) then
  2056. begin
  2057. regs:=rg[R_INTREGISTER].used_in_proc;
  2058. if current_procinfo.framepointer<>NR_NO then
  2059. begin
  2060. regs:=regs+[RS_R28,RS_R29];
  2061. LocalSize:=current_procinfo.calc_stackframe_size;
  2062. a_adjust_sp(list,LocalSize);
  2063. end;
  2064. { we clear r1 }
  2065. include(regs,getsupreg(GetDefaultZeroReg));
  2066. if current_settings.cputype<>cpu_avr1 then
  2067. begin
  2068. { Reload SREG }
  2069. regs:=regs+[getsupreg(GetDefaultTmpReg)];
  2070. cg.getcpuregister(list,GetDefaultTmpReg);
  2071. list.concat(taicpu.op_reg(A_POP, GetDefaultTmpReg));
  2072. list.concat(taicpu.op_const_reg(A_OUT, $3F, GetDefaultTmpReg));
  2073. cg.ungetcpuregister(list,GetDefaultTmpReg);
  2074. for reg:=RS_R0 to RS_R31 do
  2075. if reg in regs then
  2076. list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  2077. end;
  2078. end;
  2079. list.concat(taicpu.op_none(A_RETI));
  2080. end
  2081. else if not(nostackframe) and not(current_procinfo.procdef.isempty) then
  2082. begin
  2083. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  2084. if current_procinfo.framepointer<>NR_NO then
  2085. begin
  2086. regs:=regs+[RS_R28,RS_R29];
  2087. LocalSize:=current_procinfo.calc_stackframe_size;
  2088. a_adjust_sp(list,LocalSize);
  2089. end;
  2090. for reg:=RS_R0 to RS_R31 do
  2091. if reg in regs then
  2092. list.concat(taicpu.op_reg(A_POP,newreg(R_INTREGISTER,reg,R_SUBWHOLE)));
  2093. list.concat(taicpu.op_none(A_RET));
  2094. end
  2095. else
  2096. list.concat(taicpu.op_none(A_RET));
  2097. end;
  2098. procedure tcgavr.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  2099. var
  2100. tmpref : treference;
  2101. begin
  2102. if ref.addressmode<>AM_UNCHANGED then
  2103. internalerror(2011021701);
  2104. if assigned(ref.symbol) or (ref.offset<>0) or
  2105. { If no other reference information it must imply an absolute reference to address 0 }
  2106. ((ref.index=NR_NO) and (ref.base=NR_NO)) then
  2107. begin
  2108. reference_reset(tmpref,0,[]);
  2109. tmpref.symbol:=ref.symbol;
  2110. tmpref.offset:=ref.offset;
  2111. if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  2112. tmpref.refaddr:=addr_lo8_gs
  2113. else
  2114. tmpref.refaddr:=addr_lo8;
  2115. list.concat(taicpu.op_reg_ref(A_LDI,r,tmpref));
  2116. if assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) then
  2117. tmpref.refaddr:=addr_hi8_gs
  2118. else
  2119. tmpref.refaddr:=addr_hi8;
  2120. list.concat(taicpu.op_reg_ref(A_LDI,GetNextReg(r),tmpref));
  2121. if (ref.base<>NR_NO) then
  2122. begin
  2123. list.concat(taicpu.op_reg_reg(A_ADD,r,ref.base));
  2124. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.base)));
  2125. end;
  2126. if (ref.index<>NR_NO) then
  2127. begin
  2128. list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  2129. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  2130. end;
  2131. end
  2132. else if (ref.base<>NR_NO)then
  2133. begin
  2134. emit_mov(list,r,ref.base);
  2135. emit_mov(list,GetNextReg(r),GetNextReg(ref.base));
  2136. if (ref.index<>NR_NO) then
  2137. begin
  2138. list.concat(taicpu.op_reg_reg(A_ADD,r,ref.index));
  2139. list.concat(taicpu.op_reg_reg(A_ADC,GetNextReg(r),GetNextReg(ref.index)));
  2140. end;
  2141. end
  2142. else if (ref.index<>NR_NO) then
  2143. begin
  2144. emit_mov(list,r,ref.index);
  2145. emit_mov(list,GetNextReg(r),GetNextReg(ref.index));
  2146. end;
  2147. end;
  2148. procedure tcgavr.fixref(list : TAsmList;var ref : treference);
  2149. begin
  2150. internalerror(2011021320);
  2151. end;
  2152. procedure tcgavr.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  2153. var
  2154. paraloc1,paraloc2,paraloc3 : TCGPara;
  2155. pd : tprocdef;
  2156. begin
  2157. pd:=search_system_proc('MOVE');
  2158. paraloc1.init;
  2159. paraloc2.init;
  2160. paraloc3.init;
  2161. paramanager.getintparaloc(list,pd,1,paraloc1);
  2162. paramanager.getintparaloc(list,pd,2,paraloc2);
  2163. paramanager.getintparaloc(list,pd,3,paraloc3);
  2164. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  2165. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  2166. a_loadaddr_ref_cgpara(list,source,paraloc1);
  2167. paramanager.freecgpara(list,paraloc3);
  2168. paramanager.freecgpara(list,paraloc2);
  2169. paramanager.freecgpara(list,paraloc1);
  2170. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2171. a_call_name_static(list,'FPC_MOVE');
  2172. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2173. paraloc3.done;
  2174. paraloc2.done;
  2175. paraloc1.done;
  2176. end;
  2177. procedure tcgavr.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2178. var
  2179. countreg,tmpreg,tmpreg2: tregister;
  2180. srcref,dstref : treference;
  2181. copysize,countregsize : tcgsize;
  2182. l : TAsmLabel;
  2183. i : longint;
  2184. SrcQuickRef, DestQuickRef : Boolean;
  2185. begin
  2186. if len>16 then
  2187. begin
  2188. current_asmdata.getjumplabel(l);
  2189. reference_reset(srcref,source.alignment,source.volatility);
  2190. reference_reset(dstref,dest.alignment,source.volatility);
  2191. srcref.base:=NR_R30;
  2192. srcref.addressmode:=AM_POSTINCREMENT;
  2193. dstref.base:=NR_R26;
  2194. dstref.addressmode:=AM_POSTINCREMENT;
  2195. copysize:=OS_8;
  2196. if len<256 then
  2197. countregsize:=OS_8
  2198. else if len<65536 then
  2199. countregsize:=OS_16
  2200. else
  2201. internalerror(2011022007);
  2202. countreg:=getintregister(list,countregsize);
  2203. a_load_const_reg(list,countregsize,len,countreg);
  2204. cg.getcpuregister(list,NR_R30);
  2205. cg.getcpuregister(list,NR_R31);
  2206. a_loadaddr_ref_reg(list,source,NR_R30);
  2207. { only base or index register in dest? }
  2208. if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  2209. ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  2210. begin
  2211. if dest.base<>NR_NO then
  2212. tmpreg:=dest.base
  2213. else if dest.index<>NR_NO then
  2214. tmpreg:=dest.index
  2215. else
  2216. internalerror(2016112001);
  2217. end
  2218. else
  2219. begin
  2220. tmpreg:=getaddressregister(list);
  2221. a_loadaddr_ref_reg(list,dest,tmpreg);
  2222. end;
  2223. { X is used for spilling code so we can load it
  2224. only by a push/pop sequence, this can be
  2225. optimized later on by the peephole optimizer
  2226. }
  2227. list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  2228. list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  2229. cg.getcpuregister(list,NR_R27);
  2230. list.concat(taicpu.op_reg(A_POP,NR_R27));
  2231. cg.getcpuregister(list,NR_R26);
  2232. list.concat(taicpu.op_reg(A_POP,NR_R26));
  2233. cg.a_label(list,l);
  2234. cg.getcpuregister(list,GetDefaultTmpReg);
  2235. list.concat(taicpu.op_reg_ref(GetLoad(srcref),GetDefaultTmpReg,srcref));
  2236. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,GetDefaultTmpReg));
  2237. cg.ungetcpuregister(list,GetDefaultTmpReg);
  2238. if tcgsize2size[countregsize] = 1 then
  2239. list.concat(taicpu.op_reg(A_DEC,countreg))
  2240. else
  2241. begin
  2242. list.concat(taicpu.op_reg_const(A_SUBI,countreg,1));
  2243. list.concat(taicpu.op_reg_reg(A_SBC,GetNextReg(countreg),GetDefaultZeroReg));
  2244. end;
  2245. a_jmp_flags(list,F_NE,l);
  2246. cg.ungetcpuregister(list,NR_R26);
  2247. cg.ungetcpuregister(list,NR_R27);
  2248. cg.ungetcpuregister(list,NR_R30);
  2249. cg.ungetcpuregister(list,NR_R31);
  2250. { keep registers alive }
  2251. a_reg_sync(list,countreg);
  2252. end
  2253. else
  2254. begin
  2255. SrcQuickRef:=false;
  2256. DestQuickRef:=false;
  2257. if ((CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) and
  2258. not((source.Base=NR_NO) and (source.Index=NR_NO) and (source.Offset in [0..192-len]))) or
  2259. (
  2260. not((source.addressmode=AM_UNCHANGED) and
  2261. (source.symbol=nil) and
  2262. ((source.base=NR_R28) or
  2263. (source.base=NR_R30)) and
  2264. (source.Index=NR_NO) and
  2265. (source.Offset in [0..64-len])) and
  2266. not((source.Base=NR_NO) and (source.Index=NR_NO))
  2267. ) then
  2268. begin
  2269. cg.getcpuregister(list,NR_R30);
  2270. cg.getcpuregister(list,NR_R31);
  2271. srcref:=normalize_ref(list,source,NR_R30);
  2272. end
  2273. else
  2274. begin
  2275. SrcQuickRef:=true;
  2276. srcref:=source;
  2277. end;
  2278. if ((CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) and
  2279. not((dest.Base=NR_NO) and (dest.Index=NR_NO) and (dest.Offset in [0..192-len]))) or
  2280. (
  2281. not((dest.addressmode=AM_UNCHANGED) and
  2282. (dest.symbol=nil) and
  2283. ((dest.base=NR_R28) or
  2284. (dest.base=NR_R30)) and
  2285. (dest.Index=NR_No) and
  2286. (dest.Offset in [0..64-len])) and
  2287. not((dest.Base=NR_NO) and (dest.Index=NR_NO))
  2288. ) then
  2289. begin
  2290. if not(SrcQuickRef) then
  2291. begin
  2292. { only base or index register in dest? }
  2293. if ((dest.addressmode=AM_UNCHANGED) and (dest.offset=0) and not(assigned(dest.symbol))) and
  2294. ((dest.base<>NR_NO) xor (dest.index<>NR_NO)) then
  2295. begin
  2296. if dest.base<>NR_NO then
  2297. tmpreg:=dest.base
  2298. else if dest.index<>NR_NO then
  2299. tmpreg:=dest.index
  2300. else
  2301. internalerror(2016112002);
  2302. end
  2303. else
  2304. tmpreg:=getaddressregister(list);
  2305. dstref:=normalize_ref(list,dest,tmpreg);
  2306. { X is used for spilling code so we can load it
  2307. only by a push/pop sequence, this can be
  2308. optimized later on by the peephole optimizer
  2309. }
  2310. list.concat(taicpu.op_reg(A_PUSH,tmpreg));
  2311. list.concat(taicpu.op_reg(A_PUSH,GetNextReg(tmpreg)));
  2312. cg.getcpuregister(list,NR_R27);
  2313. list.concat(taicpu.op_reg(A_POP,NR_R27));
  2314. cg.getcpuregister(list,NR_R26);
  2315. list.concat(taicpu.op_reg(A_POP,NR_R26));
  2316. dstref.base:=NR_R26;
  2317. end
  2318. else
  2319. begin
  2320. cg.getcpuregister(list,NR_R30);
  2321. cg.getcpuregister(list,NR_R31);
  2322. dstref:=normalize_ref(list,dest,NR_R30);
  2323. end;
  2324. end
  2325. else
  2326. begin
  2327. DestQuickRef:=true;
  2328. dstref:=dest;
  2329. end;
  2330. { If dest is an ioreg and size = 16 bit then
  2331. write high byte first, then low byte
  2332. but not for avrxmega3 }
  2333. if (len = 2) and DestQuickRef and (current_settings.cputype <> cpu_avrxmega3) and
  2334. addr_is_io_register(dest.offset) then
  2335. begin
  2336. // If src is also a 16 bit ioreg then read low byte then high byte
  2337. if SrcQuickRef and addr_is_io_register(srcref.offset) then
  2338. begin
  2339. // First read source into temp registers
  2340. tmpreg:=getintregister(list, OS_16);
  2341. list.concat(taicpu.op_reg_ref(GetLoad(srcref),tmpreg,srcref));
  2342. inc(srcref.offset);
  2343. tmpreg2:=GetNextReg(tmpreg);
  2344. list.concat(taicpu.op_reg_ref(GetLoad(srcref),tmpreg2,srcref));
  2345. // then move temp registers to dest in reverse order
  2346. inc(dstref.offset);
  2347. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,tmpreg2));
  2348. dec(dstref.offset);
  2349. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,tmpreg));
  2350. end
  2351. else
  2352. begin
  2353. { avrtiny doesn't have LDD instruction, so use
  2354. predecrement version of LD with pre-incremented pointer }
  2355. if current_settings.cputype = cpu_avrtiny then
  2356. begin
  2357. srcref.addressmode:=AM_PREDECREMENT;
  2358. list.concat(taicpu.op_reg_const(A_SUBI,srcref.base,-2));
  2359. list.concat(taicpu.op_reg_const(A_SBCI,GetNextReg(srcref.base),$FF));
  2360. end
  2361. else
  2362. begin
  2363. srcref.addressmode:=AM_UNCHANGED;
  2364. inc(srcref.offset);
  2365. end;
  2366. dstref.addressmode:=AM_UNCHANGED;
  2367. inc(dstref.offset);
  2368. cg.getcpuregister(list,GetDefaultTmpReg);
  2369. list.concat(taicpu.op_reg_ref(GetLoad(srcref),GetDefaultTmpReg,srcref));
  2370. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,GetDefaultTmpReg));
  2371. cg.ungetcpuregister(list,GetDefaultTmpReg);
  2372. if not(SrcQuickRef) and (current_settings.cputype <> cpu_avrtiny) then
  2373. srcref.addressmode:=AM_POSTINCREMENT
  2374. else if current_settings.cputype = cpu_avrtiny then
  2375. srcref.addressmode:=AM_PREDECREMENT
  2376. else
  2377. srcref.addressmode:=AM_UNCHANGED;
  2378. if current_settings.cputype <> cpu_avrtiny then
  2379. dec(srcref.offset);
  2380. dec(dstref.offset);
  2381. cg.getcpuregister(list,GetDefaultTmpReg);
  2382. list.concat(taicpu.op_reg_ref(GetLoad(srcref),GetDefaultTmpReg,srcref));
  2383. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,GetDefaultTmpReg));
  2384. cg.ungetcpuregister(list,GetDefaultTmpReg);
  2385. end;
  2386. end
  2387. else
  2388. for i:=1 to len do
  2389. begin
  2390. if not(SrcQuickRef) and (i<len) then
  2391. srcref.addressmode:=AM_POSTINCREMENT
  2392. else
  2393. srcref.addressmode:=AM_UNCHANGED;
  2394. if not(DestQuickRef) and (i<len) then
  2395. dstref.addressmode:=AM_POSTINCREMENT
  2396. else
  2397. dstref.addressmode:=AM_UNCHANGED;
  2398. cg.getcpuregister(list,GetDefaultTmpReg);
  2399. list.concat(taicpu.op_reg_ref(GetLoad(srcref),GetDefaultTmpReg,srcref));
  2400. list.concat(taicpu.op_ref_reg(GetStore(dstref),dstref,GetDefaultTmpReg));
  2401. cg.ungetcpuregister(list,GetDefaultTmpReg);
  2402. if SrcQuickRef then
  2403. inc(srcref.offset);
  2404. if DestQuickRef then
  2405. inc(dstref.offset);
  2406. end;
  2407. if not(SrcQuickRef) then
  2408. begin
  2409. ungetcpuregister(list,srcref.base);
  2410. ungetcpuregister(list,TRegister(ord(srcref.base)+1));
  2411. end;
  2412. if not(DestQuickRef) then
  2413. begin
  2414. ungetcpuregister(list,dstref.base);
  2415. ungetcpuregister(list,TRegister(ord(dstref.base)+1));
  2416. end;
  2417. end;
  2418. end;
  2419. procedure tcgavr.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  2420. var
  2421. hl : tasmlabel;
  2422. ai : taicpu;
  2423. cond : TAsmCond;
  2424. begin
  2425. if not(cs_check_overflow in current_settings.localswitches) then
  2426. exit;
  2427. current_asmdata.getjumplabel(hl);
  2428. if not ((def.typ=pointerdef) or
  2429. ((def.typ=orddef) and
  2430. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2431. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2432. cond:=C_VC
  2433. else
  2434. cond:=C_CC;
  2435. ai:=Taicpu.Op_Sym(A_BRxx,hl);
  2436. ai.SetCondition(cond);
  2437. ai.is_jmp:=true;
  2438. list.concat(ai);
  2439. a_call_name(list,'FPC_OVERFLOW',false);
  2440. a_label(list,hl);
  2441. end;
  2442. procedure tcgavr.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; ovloc: tlocation);
  2443. var
  2444. hl : tasmlabel;
  2445. ai : taicpu;
  2446. cond : TAsmCond;
  2447. begin
  2448. if not(cs_check_overflow in current_settings.localswitches) then
  2449. exit;
  2450. case ovloc.loc of
  2451. LOC_FLAGS:
  2452. begin
  2453. current_asmdata.getjumplabel(hl);
  2454. if not ((def.typ=pointerdef) or
  2455. ((def.typ=orddef) and
  2456. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2457. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2458. cond:=C_VC
  2459. else
  2460. cond:=C_CC;
  2461. ai:=Taicpu.Op_Sym(A_BRxx,hl);
  2462. ai.SetCondition(cond);
  2463. ai.is_jmp:=true;
  2464. list.concat(ai);
  2465. a_call_name(list,'FPC_OVERFLOW',false);
  2466. a_label(list,hl);
  2467. end;
  2468. end;
  2469. end;
  2470. procedure tcgavr.g_save_registers(list: TAsmList);
  2471. begin
  2472. { this is done by the entry code }
  2473. end;
  2474. procedure tcgavr.g_restore_registers(list: TAsmList);
  2475. begin
  2476. { this is done by the exit code }
  2477. end;
  2478. procedure tcgavr.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2479. var
  2480. ai1,ai2 : taicpu;
  2481. hl : TAsmLabel;
  2482. begin
  2483. ai1:=Taicpu.Op_sym(A_BRxx,l);
  2484. ai1.is_jmp:=true;
  2485. hl:=nil;
  2486. case cond of
  2487. OC_EQ:
  2488. ai1.SetCondition(C_EQ);
  2489. OC_GT:
  2490. begin
  2491. { emulate GT }
  2492. current_asmdata.getjumplabel(hl);
  2493. ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  2494. ai2.SetCondition(C_EQ);
  2495. ai2.is_jmp:=true;
  2496. list.concat(ai2);
  2497. ai1.SetCondition(C_GE);
  2498. end;
  2499. OC_LT:
  2500. ai1.SetCondition(C_LT);
  2501. OC_GTE:
  2502. ai1.SetCondition(C_GE);
  2503. OC_LTE:
  2504. begin
  2505. { emulate LTE }
  2506. ai2:=Taicpu.Op_Sym(A_BRxx,l);
  2507. ai2.SetCondition(C_EQ);
  2508. ai2.is_jmp:=true;
  2509. list.concat(ai2);
  2510. ai1.SetCondition(C_LT);
  2511. end;
  2512. OC_NE:
  2513. ai1.SetCondition(C_NE);
  2514. OC_BE:
  2515. begin
  2516. { emulate BE }
  2517. ai2:=Taicpu.Op_Sym(A_BRxx,l);
  2518. ai2.SetCondition(C_EQ);
  2519. ai2.is_jmp:=true;
  2520. list.concat(ai2);
  2521. ai1.SetCondition(C_LO);
  2522. end;
  2523. OC_B:
  2524. ai1.SetCondition(C_LO);
  2525. OC_AE:
  2526. ai1.SetCondition(C_SH);
  2527. OC_A:
  2528. begin
  2529. { emulate A (unsigned GT) }
  2530. current_asmdata.getjumplabel(hl);
  2531. ai2:=Taicpu.Op_Sym(A_BRxx,hl);
  2532. ai2.SetCondition(C_EQ);
  2533. ai2.is_jmp:=true;
  2534. list.concat(ai2);
  2535. ai1.SetCondition(C_SH);
  2536. end;
  2537. else
  2538. internalerror(2011082501);
  2539. end;
  2540. list.concat(ai1);
  2541. if assigned(hl) then
  2542. a_label(list,hl);
  2543. end;
  2544. procedure tcgavr.emit_mov(list: TAsmList;reg2: tregister; reg1: tregister);
  2545. var
  2546. instr: taicpu;
  2547. begin
  2548. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  2549. list.Concat(instr);
  2550. { Notify the register allocator that we have written a move instruction so
  2551. it can try to eliminate it. }
  2552. add_move_instruction(instr);
  2553. end;
  2554. procedure tcg64favr.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2555. begin
  2556. if not(size in [OS_S64,OS_64]) then
  2557. internalerror(2012102402);
  2558. tcgavr(cg).a_op_reg_reg_internal(list,Op,size,regsrc.reglo,regsrc.reghi,regdst.reglo,regdst.reghi);
  2559. end;
  2560. procedure tcg64favr.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2561. begin
  2562. tcgavr(cg).a_op_const_reg_internal(list,Op,size,value,reg.reglo,reg.reghi);
  2563. end;
  2564. procedure tcg64favr.a_op64_const_reg_reg(list: TAsmList; op: TOpCg;size: tcgsize;value: int64;src,dst : tregister64);
  2565. begin
  2566. if op in [OP_SHL,OP_SHR] then
  2567. tcgavr(cg).a_op_const_reg_reg_internal(list,Op,size,value,src.reglo,src.reghi,dst.reglo,dst.reghi)
  2568. else
  2569. Inherited a_op64_const_reg_reg(list,op,size,value,src,dst);
  2570. end;
  2571. procedure create_codegen;
  2572. begin
  2573. cg:=tcgavr.create;
  2574. cg64:=tcg64favr.create;
  2575. end;
  2576. end.