2
0

cgcpu.pas 82 KB

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