2
0

cgcpu.pas 66 KB

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