ncgutil.pas 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bitalu}
  27. ,cg64f32
  28. {$endif not cpu64bitalu}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. pusedregvars = ^tusedregvars;
  33. tusedregvars = record
  34. intregvars, fpuregvars, mmregvars: Tsuperregisterworklist;
  35. end;
  36. {
  37. Not used currently, implemented because I thought we had to
  38. synchronise around if/then/else as well, but not needed. May
  39. still be useful for SSA once we get around to implementing
  40. that (JM)
  41. pusedregvarscommon = ^tusedregvarscommon;
  42. tusedregvarscommon = record
  43. allregvars, commonregvars, myregvars: tusedregvars;
  44. end;
  45. }
  46. procedure firstcomplex(p : tbinarynode);
  47. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  48. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  49. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  50. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  51. procedure location_force_mem(list:TAsmList;var l:tlocation);
  52. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  53. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  54. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  55. { Retrieve the location of the data pointed to in location l, when the location is
  56. a register it is expected to contain the address of the data }
  57. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);
  58. function has_alias_name(pd:tprocdef;const s:string):boolean;
  59. procedure alloc_proc_symbol(pd: tprocdef);
  60. procedure gen_proc_symbol(list:TAsmList);
  61. procedure gen_proc_symbol_end(list:TAsmList);
  62. procedure gen_proc_entry_code(list:TAsmList);
  63. procedure gen_proc_exit_code(list:TAsmList);
  64. procedure gen_stack_check_size_para(list:TAsmList);
  65. procedure gen_stack_check_call(list:TAsmList);
  66. procedure gen_save_used_regs(list:TAsmList);
  67. procedure gen_restore_used_regs(list:TAsmList);
  68. procedure gen_initialize_code(list:TAsmList);
  69. procedure gen_finalize_code(list:TAsmList);
  70. procedure gen_entry_code(list:TAsmList);
  71. procedure gen_exit_code(list:TAsmList);
  72. procedure gen_load_para_value(list:TAsmList);
  73. procedure gen_load_return_value(list:TAsmList);
  74. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  75. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  76. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  77. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  78. { adds the regvars used in n and its children to rv.allregvars,
  79. those which were already in rv.allregvars to rv.commonregvars and
  80. uses rv.myregvars as scratch (so that two uses of the same regvar
  81. in a single tree to make it appear in commonregvars). Useful to
  82. find out which regvars are used in two different node trees
  83. (e.g. in the "else" and "then" path, or in various case blocks }
  84. // procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  85. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  86. { if the result of n is a LOC_C(..)REGISTER, try to find the corresponding }
  87. { loadn and change its location to a new register (= SSA). In case reload }
  88. { is true, transfer the old to the new register }
  89. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  90. {#
  91. Allocate the buffers for exception management and setjmp environment.
  92. Return a pointer to these buffers, send them to the utility routine
  93. so they are registered, and then call setjmp.
  94. Then compare the result of setjmp with 0, and if not equal
  95. to zero, then jump to exceptlabel.
  96. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  97. It is to note that this routine may be called *after* the stackframe of a
  98. routine has been called, therefore on machines where the stack cannot
  99. be modified, all temps should be allocated on the heap instead of the
  100. stack.
  101. }
  102. const
  103. EXCEPT_BUF_SIZE = 3*sizeof(pint);
  104. type
  105. texceptiontemps=record
  106. jmpbuf,
  107. envbuf,
  108. reasonbuf : treference;
  109. end;
  110. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  111. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  112. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  113. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  114. procedure insertbssdata(sym : tstaticvarsym);
  115. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  116. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  117. procedure location_free(list: TAsmList; const location : TLocation);
  118. function getprocalign : shortint;
  119. procedure gen_pic_helpers(list : TAsmList);
  120. implementation
  121. uses
  122. version,
  123. cutils,cclasses,
  124. globals,systems,verbose,export,
  125. ppu,defutil,
  126. procinfo,paramgr,fmodule,
  127. regvars,dbgbase,
  128. pass_1,pass_2,
  129. nbas,ncon,nld,nmem,nutils,
  130. tgobj,cgobj
  131. {$ifdef powerpc}
  132. , cpupi
  133. {$endif}
  134. {$ifdef powerpc64}
  135. , cpupi
  136. {$endif}
  137. {$ifdef SUPPORT_MMX}
  138. , cgx86
  139. {$endif SUPPORT_MMX}
  140. ;
  141. {*****************************************************************************
  142. Misc Helpers
  143. *****************************************************************************}
  144. procedure location_free(list: TAsmList; const location : TLocation);
  145. begin
  146. case location.loc of
  147. LOC_VOID:
  148. ;
  149. LOC_REGISTER,
  150. LOC_CREGISTER:
  151. begin
  152. if getsupreg(location.register)<first_int_imreg then
  153. cg.ungetcpuregister(list,location.register);
  154. end;
  155. LOC_FPUREGISTER,
  156. LOC_CFPUREGISTER:
  157. begin
  158. if getsupreg(location.register)<first_fpu_imreg then
  159. cg.ungetcpuregister(list,location.register);
  160. end;
  161. LOC_MMREGISTER,
  162. LOC_CMMREGISTER :
  163. begin
  164. if getsupreg(location.register)<first_mm_imreg then
  165. cg.ungetcpuregister(list,location.register);
  166. end;
  167. LOC_REFERENCE,
  168. LOC_CREFERENCE :
  169. begin
  170. if use_fixed_stack then
  171. location_freetemp(list,location);
  172. end;
  173. else
  174. internalerror(2004110211);
  175. end;
  176. end;
  177. procedure firstcomplex(p : tbinarynode);
  178. var
  179. fcl, fcr: longint;
  180. ncl, ncr: longint;
  181. begin
  182. { always calculate boolean AND and OR from left to right }
  183. if (p.nodetype in [orn,andn]) and
  184. is_boolean(p.left.resultdef) then
  185. begin
  186. if nf_swapped in p.flags then
  187. internalerror(200709253);
  188. end
  189. else
  190. begin
  191. fcl:=node_resources_fpu(p.left);
  192. fcr:=node_resources_fpu(p.right);
  193. ncl:=node_complexity(p.left);
  194. ncr:=node_complexity(p.right);
  195. { We swap left and right if
  196. a) right needs more floating point registers than left, and
  197. left needs more than 0 floating point registers (if it
  198. doesn't need any, swapping won't change the floating
  199. point register pressure)
  200. b) both left and right need an equal amount of floating
  201. point registers or right needs no floating point registers,
  202. and in addition right has a higher complexity than left
  203. (+- needs more integer registers, but not necessarily)
  204. }
  205. if ((fcr>fcl) and
  206. (fcl>0)) or
  207. (((fcr=fcl) or
  208. (fcr=0)) and
  209. (ncr>ncl)) then
  210. p.swapleftright
  211. end;
  212. end;
  213. procedure maketojumpbool(list:TAsmList; p : tnode; loadregvars: tloadregvars);
  214. {
  215. produces jumps to true respectively false labels using boolean expressions
  216. depending on whether the loading of regvars is currently being
  217. synchronized manually (such as in an if-node) or automatically (most of
  218. the other cases where this procedure is called), loadregvars can be
  219. "lr_load_regvars" or "lr_dont_load_regvars"
  220. }
  221. var
  222. opsize : tcgsize;
  223. storepos : tfileposinfo;
  224. tmpreg : tregister;
  225. begin
  226. if nf_error in p.flags then
  227. exit;
  228. storepos:=current_filepos;
  229. current_filepos:=p.fileinfo;
  230. if is_boolean(p.resultdef) then
  231. begin
  232. {$ifdef OLDREGVARS}
  233. if loadregvars = lr_load_regvars then
  234. load_all_regvars(list);
  235. {$endif OLDREGVARS}
  236. if is_constboolnode(p) then
  237. begin
  238. if Tordconstnode(p).value.uvalue<>0 then
  239. cg.a_jmp_always(list,current_procinfo.CurrTrueLabel)
  240. else
  241. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel)
  242. end
  243. else
  244. begin
  245. opsize:=def_cgsize(p.resultdef);
  246. case p.location.loc of
  247. LOC_SUBSETREG,LOC_CSUBSETREG,
  248. LOC_SUBSETREF,LOC_CSUBSETREF:
  249. begin
  250. tmpreg := cg.getintregister(list,OS_INT);
  251. cg.a_load_loc_reg(list,OS_INT,p.location,tmpreg);
  252. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,tmpreg,current_procinfo.CurrTrueLabel);
  253. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  254. end;
  255. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  256. begin
  257. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,current_procinfo.CurrTrueLabel);
  258. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  259. end;
  260. LOC_JUMP:
  261. ;
  262. {$ifdef cpuflags}
  263. LOC_FLAGS :
  264. begin
  265. cg.a_jmp_flags(list,p.location.resflags,current_procinfo.CurrTrueLabel);
  266. cg.a_jmp_always(list,current_procinfo.CurrFalseLabel);
  267. end;
  268. {$endif cpuflags}
  269. else
  270. begin
  271. printnode(output,p);
  272. internalerror(200308241);
  273. end;
  274. end;
  275. end;
  276. end
  277. else
  278. internalerror(200112305);
  279. current_filepos:=storepos;
  280. end;
  281. (*
  282. This code needs fixing. It is not safe to use rgint; on the m68000 it
  283. would be rgaddr.
  284. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  285. begin
  286. case t.loc of
  287. LOC_REGISTER:
  288. begin
  289. { can't be a regvar, since it would be LOC_CREGISTER then }
  290. exclude(regs,getsupreg(t.register));
  291. if t.register64.reghi<>NR_NO then
  292. exclude(regs,getsupreg(t.register64.reghi));
  293. end;
  294. LOC_CREFERENCE,LOC_REFERENCE:
  295. begin
  296. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  297. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  298. exclude(regs,getsupreg(t.reference.base));
  299. if not(cs_opt_regvar in current_settings.optimizerswitches) or
  300. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  301. exclude(regs,getsupreg(t.reference.index));
  302. end;
  303. end;
  304. end;
  305. *)
  306. {*****************************************************************************
  307. EXCEPTION MANAGEMENT
  308. *****************************************************************************}
  309. procedure get_exception_temps(list:TAsmList;var t:texceptiontemps);
  310. var
  311. srsym : ttypesym;
  312. begin
  313. if jmp_buf_size=-1 then
  314. begin
  315. srsym:=search_system_type('JMP_BUF');
  316. jmp_buf_size:=srsym.typedef.size;
  317. jmp_buf_align:=srsym.typedef.alignment;
  318. end;
  319. tg.GetTemp(list,EXCEPT_BUF_SIZE,sizeof(pint),tt_persistent,t.envbuf);
  320. tg.GetTemp(list,jmp_buf_size,jmp_buf_align,tt_persistent,t.jmpbuf);
  321. tg.GetTemp(list,sizeof(pint),sizeof(pint),tt_persistent,t.reasonbuf);
  322. end;
  323. procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  324. begin
  325. tg.Ungettemp(list,t.jmpbuf);
  326. tg.ungettemp(list,t.envbuf);
  327. tg.ungettemp(list,t.reasonbuf);
  328. end;
  329. procedure new_exception(list:TAsmList;const t:texceptiontemps;exceptlabel:tasmlabel);
  330. var
  331. paraloc1,paraloc2,paraloc3 : tcgpara;
  332. begin
  333. paraloc1.init;
  334. paraloc2.init;
  335. paraloc3.init;
  336. paramanager.getintparaloc(pocall_default,1,paraloc1);
  337. paramanager.getintparaloc(pocall_default,2,paraloc2);
  338. paramanager.getintparaloc(pocall_default,3,paraloc3);
  339. paramanager.allocparaloc(list,paraloc3);
  340. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  341. paramanager.allocparaloc(list,paraloc2);
  342. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  343. { push type of exceptionframe }
  344. paramanager.allocparaloc(list,paraloc1);
  345. cg.a_param_const(list,OS_S32,1,paraloc1);
  346. paramanager.freeparaloc(list,paraloc3);
  347. paramanager.freeparaloc(list,paraloc2);
  348. paramanager.freeparaloc(list,paraloc1);
  349. cg.allocallcpuregisters(list);
  350. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR',false);
  351. cg.deallocallcpuregisters(list);
  352. paramanager.getintparaloc(pocall_default,1,paraloc1);
  353. paramanager.allocparaloc(list,paraloc1);
  354. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  355. paramanager.freeparaloc(list,paraloc1);
  356. cg.allocallcpuregisters(list);
  357. cg.a_call_name(list,'FPC_SETJMP',false);
  358. cg.deallocallcpuregisters(list);
  359. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  360. cg.g_exception_reason_save(list, t.reasonbuf);
  361. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  362. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  363. paraloc1.done;
  364. paraloc2.done;
  365. paraloc3.done;
  366. end;
  367. procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  368. begin
  369. cg.allocallcpuregisters(list);
  370. cg.a_call_name(list,'FPC_POPADDRSTACK',false);
  371. cg.deallocallcpuregisters(list);
  372. if not onlyfree then
  373. begin
  374. cg.alloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  375. cg.g_exception_reason_load(list, t.reasonbuf);
  376. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  377. cg.dealloccpuregisters(list,R_INTREGISTER,[RS_FUNCTION_RESULT_REG]);
  378. end;
  379. end;
  380. {*****************************************************************************
  381. TLocation
  382. *****************************************************************************}
  383. {$ifndef cpu64bitalu}
  384. { 32-bit version }
  385. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  386. var
  387. hregister,
  388. hregisterhi : tregister;
  389. hreg64 : tregister64;
  390. hl : tasmlabel;
  391. oldloc : tlocation;
  392. const_location: boolean;
  393. begin
  394. oldloc:=l;
  395. if dst_size=OS_NO then
  396. internalerror(200309144);
  397. { handle transformations to 64bit separate }
  398. if dst_size in [OS_64,OS_S64] then
  399. begin
  400. if not (l.size in [OS_64,OS_S64]) then
  401. begin
  402. { load a smaller size to OS_64 }
  403. if l.loc=LOC_REGISTER then
  404. begin
  405. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  406. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  407. end
  408. else
  409. hregister:=cg.getintregister(list,OS_INT);
  410. { load value in low register }
  411. case l.loc of
  412. LOC_FLAGS :
  413. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  414. LOC_JUMP :
  415. begin
  416. cg.a_label(list,current_procinfo.CurrTrueLabel);
  417. cg.a_load_const_reg(list,OS_INT,1,hregister);
  418. current_asmdata.getjumplabel(hl);
  419. cg.a_jmp_always(list,hl);
  420. cg.a_label(list,current_procinfo.CurrFalseLabel);
  421. cg.a_load_const_reg(list,OS_INT,0,hregister);
  422. cg.a_label(list,hl);
  423. end;
  424. else
  425. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  426. end;
  427. { reset hi part, take care of the signed bit of the current value }
  428. hregisterhi:=cg.getintregister(list,OS_INT);
  429. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  430. begin
  431. if l.loc=LOC_CONSTANT then
  432. begin
  433. if (longint(l.value)<0) then
  434. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  435. else
  436. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  437. end
  438. else
  439. begin
  440. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  441. hregisterhi);
  442. end;
  443. end
  444. else
  445. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  446. location_reset(l,LOC_REGISTER,dst_size);
  447. l.register64.reglo:=hregister;
  448. l.register64.reghi:=hregisterhi;
  449. end
  450. else
  451. begin
  452. { 64bit to 64bit }
  453. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  454. begin
  455. hregister:=l.register64.reglo;
  456. hregisterhi:=l.register64.reghi;
  457. const_location := true;
  458. end
  459. else
  460. begin
  461. hregister:=cg.getintregister(list,OS_INT);
  462. hregisterhi:=cg.getintregister(list,OS_INT);
  463. const_location := false;
  464. end;
  465. hreg64.reglo:=hregister;
  466. hreg64.reghi:=hregisterhi;
  467. { load value in new register }
  468. cg64.a_load64_loc_reg(list,l,hreg64);
  469. if not const_location then
  470. location_reset(l,LOC_REGISTER,dst_size)
  471. else
  472. location_reset(l,LOC_CREGISTER,dst_size);
  473. l.register64.reglo:=hregister;
  474. l.register64.reghi:=hregisterhi;
  475. end;
  476. end
  477. else
  478. begin
  479. {Do not bother to recycle the existing register. The register
  480. allocator eliminates unnecessary moves, so it's not needed
  481. and trying to recycle registers can cause problems because
  482. the registers changes size and may need aditional constraints.
  483. Not if it's about LOC_CREGISTER's (JM)
  484. }
  485. const_location :=
  486. (maybeconst) and
  487. (l.loc = LOC_CREGISTER) and
  488. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  489. ((l.size = dst_size) or
  490. (TCGSize2Size[l.size] = sizeof(aint)));
  491. if not const_location then
  492. hregister:=cg.getintregister(list,dst_size)
  493. else
  494. hregister := l.register;
  495. { load value in new register }
  496. case l.loc of
  497. LOC_FLAGS :
  498. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  499. LOC_JUMP :
  500. begin
  501. cg.a_label(list,current_procinfo.CurrTrueLabel);
  502. cg.a_load_const_reg(list,dst_size,1,hregister);
  503. current_asmdata.getjumplabel(hl);
  504. cg.a_jmp_always(list,hl);
  505. cg.a_label(list,current_procinfo.CurrFalseLabel);
  506. cg.a_load_const_reg(list,dst_size,0,hregister);
  507. cg.a_label(list,hl);
  508. end;
  509. else
  510. begin
  511. { load_loc_reg can only handle size >= l.size, when the
  512. new size is smaller then we need to adjust the size
  513. of the orignal and maybe recalculate l.register for i386 }
  514. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  515. begin
  516. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  517. l.register:=cg.makeregsize(list,l.register,dst_size);
  518. { for big endian systems, the reference's offset must }
  519. { be increased in this case, since they have the }
  520. { MSB first in memory and e.g. byte(word_var) should }
  521. { return the second byte in this case (JM) }
  522. if (target_info.endian = ENDIAN_BIG) and
  523. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  524. begin
  525. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  526. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  527. end;
  528. {$ifdef x86}
  529. if not (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  530. l.size:=dst_size;
  531. {$endif x86}
  532. end;
  533. cg.a_load_loc_reg(list,dst_size,l,hregister);
  534. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size])
  535. {$ifdef x86}
  536. and (l.loc in [LOC_SUBSETREG,LOC_CSUBSETREG])
  537. {$endif x86}
  538. then
  539. l.size:=dst_size;
  540. end;
  541. end;
  542. if not const_location then
  543. location_reset(l,LOC_REGISTER,dst_size)
  544. else
  545. location_reset(l,LOC_CREGISTER,dst_size);
  546. l.register:=hregister;
  547. end;
  548. { Release temp when it was a reference }
  549. if oldloc.loc=LOC_REFERENCE then
  550. location_freetemp(list,oldloc);
  551. end;
  552. {$else not cpu64bitalu}
  553. { 64-bit version }
  554. procedure location_force_reg(list:TAsmList;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  555. var
  556. hregister : tregister;
  557. hl : tasmlabel;
  558. oldloc : tlocation;
  559. begin
  560. oldloc:=l;
  561. hregister:=cg.getintregister(list,dst_size);
  562. { load value in new register }
  563. case l.loc of
  564. LOC_FLAGS :
  565. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  566. LOC_JUMP :
  567. begin
  568. cg.a_label(list,current_procinfo.CurrTrueLabel);
  569. cg.a_load_const_reg(list,dst_size,1,hregister);
  570. current_asmdata.getjumplabel(hl);
  571. cg.a_jmp_always(list,hl);
  572. cg.a_label(list,current_procinfo.CurrFalseLabel);
  573. cg.a_load_const_reg(list,dst_size,0,hregister);
  574. cg.a_label(list,hl);
  575. end;
  576. else
  577. begin
  578. { load_loc_reg can only handle size >= l.size, when the
  579. new size is smaller then we need to adjust the size
  580. of the orignal and maybe recalculate l.register for i386 }
  581. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  582. begin
  583. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  584. l.register:=cg.makeregsize(list,l.register,dst_size);
  585. { for big endian systems, the reference's offset must }
  586. { be increased in this case, since they have the }
  587. { MSB first in memory and e.g. byte(word_var) should }
  588. { return the second byte in this case (JM) }
  589. if (target_info.endian = ENDIAN_BIG) and
  590. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  591. begin
  592. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  593. l.reference.alignment:=newalignment(l.reference.alignment,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  594. end;
  595. {$ifdef x86}
  596. l.size:=dst_size;
  597. {$endif x86}
  598. end;
  599. cg.a_load_loc_reg(list,dst_size,l,hregister);
  600. {$ifndef x86}
  601. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  602. l.size:=dst_size;
  603. {$endif not x86}
  604. end;
  605. end;
  606. if (l.loc <> LOC_CREGISTER) or
  607. not maybeconst then
  608. location_reset(l,LOC_REGISTER,dst_size)
  609. else
  610. location_reset(l,LOC_CREGISTER,dst_size);
  611. l.register:=hregister;
  612. { Release temp when it was a reference }
  613. if oldloc.loc=LOC_REFERENCE then
  614. location_freetemp(list,oldloc);
  615. end;
  616. {$endif not cpu64bitalu}
  617. procedure location_force_fpureg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  618. var
  619. reg : tregister;
  620. href : treference;
  621. begin
  622. if (l.loc<>LOC_FPUREGISTER) and
  623. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  624. begin
  625. { if it's in an mm register, store to memory first }
  626. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  627. begin
  628. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  629. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  630. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  631. l.reference:=href;
  632. end;
  633. reg:=cg.getfpuregister(list,l.size);
  634. cg.a_loadfpu_loc_reg(list,l.size,l,reg);
  635. location_freetemp(list,l);
  636. location_reset(l,LOC_FPUREGISTER,l.size);
  637. l.register:=reg;
  638. end;
  639. end;
  640. procedure location_force_mmregscalar(list:TAsmList;var l: tlocation;maybeconst:boolean);
  641. var
  642. reg : tregister;
  643. href : treference;
  644. begin
  645. if (l.loc<>LOC_MMREGISTER) and
  646. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  647. begin
  648. { if it's in an fpu register, store to memory first }
  649. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  650. begin
  651. tg.GetTemp(list,tcgsize2size[l.size],tcgsize2size[l.size],tt_normal,href);
  652. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,href);
  653. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  654. l.reference:=href;
  655. end;
  656. reg:=cg.getmmregister(list,l.size);
  657. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  658. location_freetemp(list,l);
  659. location_reset(l,LOC_MMREGISTER,l.size);
  660. l.register:=reg;
  661. end;
  662. end;
  663. procedure register_maybe_adjust_setbase(list: TAsmList; var l: tlocation; setbase: aint);
  664. var
  665. tmpreg: tregister;
  666. begin
  667. if (setbase<>0) then
  668. begin
  669. if not(l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  670. internalerror(2007091502);
  671. { subtract the setbase }
  672. case l.loc of
  673. LOC_CREGISTER:
  674. begin
  675. tmpreg := cg.getintregister(list,l.size);
  676. cg.a_op_const_reg_reg(list,OP_SUB,l.size,setbase,l.register,tmpreg);
  677. l.loc:=LOC_REGISTER;
  678. l.register:=tmpreg;
  679. end;
  680. LOC_REGISTER:
  681. begin
  682. cg.a_op_const_reg(list,OP_SUB,l.size,setbase,l.register);
  683. end;
  684. end;
  685. end;
  686. end;
  687. procedure location_force_mmreg(list:TAsmList;var l: tlocation;maybeconst:boolean);
  688. var
  689. reg : tregister;
  690. begin
  691. if (l.loc<>LOC_MMREGISTER) and
  692. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  693. begin
  694. reg:=cg.getmmregister(list,OS_VECTOR);
  695. cg.a_loadmm_loc_reg(list,OS_VECTOR,l,reg,nil);
  696. location_freetemp(list,l);
  697. location_reset(l,LOC_MMREGISTER,OS_VECTOR);
  698. l.register:=reg;
  699. end;
  700. end;
  701. procedure location_force_mem(list:TAsmList;var l:tlocation);
  702. var
  703. r : treference;
  704. begin
  705. case l.loc of
  706. LOC_FPUREGISTER,
  707. LOC_CFPUREGISTER :
  708. begin
  709. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  710. cg.a_loadfpu_reg_ref(list,l.size,l.size,l.register,r);
  711. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  712. l.reference:=r;
  713. end;
  714. LOC_MMREGISTER,
  715. LOC_CMMREGISTER:
  716. begin
  717. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  718. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  719. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  720. l.reference:=r;
  721. end;
  722. LOC_CONSTANT,
  723. LOC_REGISTER,
  724. LOC_CREGISTER :
  725. begin
  726. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  727. {$ifndef cpu64bitalu}
  728. if l.size in [OS_64,OS_S64] then
  729. cg64.a_load64_loc_ref(list,l,r)
  730. else
  731. {$endif not cpu64bitalu}
  732. cg.a_load_loc_ref(list,l.size,l,r);
  733. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  734. l.reference:=r;
  735. end;
  736. LOC_SUBSETREG,
  737. LOC_CSUBSETREG,
  738. LOC_SUBSETREF,
  739. LOC_CSUBSETREF:
  740. begin
  741. tg.GetTemp(list,TCGSize2Size[l.size],TCGSize2Size[l.size],tt_normal,r);
  742. cg.a_load_loc_ref(list,l.size,l,r);
  743. location_reset_ref(l,LOC_REFERENCE,l.size,0);
  744. l.reference:=r;
  745. end;
  746. LOC_CREFERENCE,
  747. LOC_REFERENCE : ;
  748. else
  749. internalerror(200203219);
  750. end;
  751. end;
  752. procedure location_get_data_ref(list:TAsmList;const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);
  753. begin
  754. case l.loc of
  755. LOC_REGISTER,
  756. LOC_CREGISTER :
  757. begin
  758. if not loadref then
  759. internalerror(200410231);
  760. reference_reset_base(ref,l.register,0,alignment);
  761. end;
  762. LOC_REFERENCE,
  763. LOC_CREFERENCE :
  764. begin
  765. if loadref then
  766. begin
  767. reference_reset_base(ref,cg.getaddressregister(list),0,alignment);
  768. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  769. end
  770. else
  771. ref:=l.reference;
  772. end;
  773. else
  774. internalerror(200309181);
  775. end;
  776. end;
  777. {****************************************************************************
  778. Init/Finalize Code
  779. ****************************************************************************}
  780. procedure copyvalueparas(p:TObject;arg:pointer);
  781. var
  782. href : treference;
  783. hreg : tregister;
  784. list : TAsmList;
  785. hsym : tparavarsym;
  786. l : longint;
  787. localcopyloc : tlocation;
  788. begin
  789. list:=TAsmList(arg);
  790. if (tsym(p).typ=paravarsym) and
  791. (tparavarsym(p).varspez=vs_value) and
  792. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  793. begin
  794. { we have no idea about the alignment at the caller side }
  795. location_get_data_ref(list,tparavarsym(p).initialloc,href,true,1);
  796. if is_open_array(tparavarsym(p).vardef) or
  797. is_array_of_const(tparavarsym(p).vardef) then
  798. begin
  799. { cdecl functions don't have a high pointer so it is not possible to generate
  800. a local copy }
  801. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  802. begin
  803. hsym:=tparavarsym(tsym(p).owner.Find('high'+tsym(p).name));
  804. if not assigned(hsym) then
  805. internalerror(200306061);
  806. hreg:=cg.getaddressregister(list);
  807. if not is_packed_array(tparavarsym(p).vardef) then
  808. cg.g_copyvaluepara_openarray(list,href,hsym.initialloc,tarraydef(tparavarsym(p).vardef).elesize,hreg)
  809. else
  810. internalerror(2006080401);
  811. // cg.g_copyvaluepara_packedopenarray(list,href,hsym.intialloc,tarraydef(tparavarsym(p).vardef).elepackedbitsize,hreg);
  812. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).initialloc);
  813. end;
  814. end
  815. else
  816. begin
  817. { Allocate space for the local copy }
  818. l:=tparavarsym(p).getsize;
  819. localcopyloc.loc:=LOC_REFERENCE;
  820. localcopyloc.size:=int_cgsize(l);
  821. tg.GetLocal(list,l,tparavarsym(p).vardef,localcopyloc.reference);
  822. { Copy data }
  823. if is_shortstring(tparavarsym(p).vardef) then
  824. begin
  825. { this code is only executed before the code for the body and the entry/exit code is generated
  826. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  827. }
  828. include(current_procinfo.flags,pi_do_call);
  829. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vardef).len)
  830. end
  831. else if tparavarsym(p).vardef.typ = variantdef then
  832. begin
  833. { this code is only executed before the code for the body and the entry/exit code is generated
  834. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  835. }
  836. include(current_procinfo.flags,pi_do_call);
  837. cg.g_copyvariant(list,href,localcopyloc.reference)
  838. end
  839. else
  840. begin
  841. { pass proper alignment info }
  842. localcopyloc.reference.alignment:=tparavarsym(p).vardef.alignment;
  843. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vardef.size);
  844. end;
  845. { update localloc of varsym }
  846. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  847. tparavarsym(p).localloc:=localcopyloc;
  848. tparavarsym(p).initialloc:=localcopyloc;
  849. end;
  850. end;
  851. end;
  852. const
  853. {$ifdef cpu64bitalu}
  854. trashintvalues: array[0..nroftrashvalues-1] of aint = ($5555555555555555,aint($AAAAAAAAAAAAAAAA),aint($EFEFEFEFEFEFEFEF),0);
  855. {$else cpu64bitalu}
  856. trashintvalues: array[0..nroftrashvalues-1] of aint = ($55555555,aint($AAAAAAAA),aint($EFEFEFEF),0);
  857. {$endif cpu64bitalu}
  858. procedure trash_reference(list: TAsmList; const ref: treference; size: aint);
  859. var
  860. countreg, valuereg: tregister;
  861. hl: tasmlabel;
  862. trashintval: aint;
  863. tmpref: treference;
  864. begin
  865. trashintval := trashintvalues[localvartrashing];
  866. case size of
  867. 0: ; { empty record }
  868. 1: cg.a_load_const_ref(list,OS_8,byte(trashintval),ref);
  869. 2: cg.a_load_const_ref(list,OS_16,word(trashintval),ref);
  870. 4: cg.a_load_const_ref(list,OS_32,longint(trashintval),ref);
  871. {$ifdef cpu64bitalu}
  872. 8: cg.a_load_const_ref(list,OS_64,int64(trashintval),ref);
  873. {$endif cpu64bitalu}
  874. else
  875. begin
  876. countreg := cg.getintregister(list,OS_ADDR);
  877. valuereg := cg.getintregister(list,OS_8);
  878. cg.a_load_const_reg(list,OS_INT,size,countreg);
  879. cg.a_load_const_reg(list,OS_8,byte(trashintval),valuereg);
  880. current_asmdata.getjumplabel(hl);
  881. tmpref := ref;
  882. if (tmpref.index <> NR_NO) then
  883. internalerror(200607201);
  884. tmpref.index := countreg;
  885. dec(tmpref.offset);
  886. cg.a_label(list,hl);
  887. cg.a_load_reg_ref(list,OS_8,OS_8,valuereg,tmpref);
  888. cg.a_op_const_reg(list,OP_SUB,OS_INT,1,countreg);
  889. cg.a_cmp_const_reg_label(list,OS_INT,OC_NE,0,countreg,hl);
  890. cg.a_reg_sync(list,tmpref.base);
  891. cg.a_reg_sync(list,valuereg);
  892. end;
  893. end;
  894. end;
  895. { trash contents of local variables or parameters (function result) }
  896. procedure trash_variable(p:TObject;arg:pointer);
  897. var
  898. trashintval: aint;
  899. list: TAsmList absolute arg;
  900. begin
  901. if ((tsym(p).typ=localvarsym) or
  902. ((tsym(p).typ=paravarsym) and
  903. (vo_is_funcret in tparavarsym(p).varoptions))) and
  904. not(tabstractnormalvarsym(p).vardef.needs_inittable) and
  905. not(assigned(tabstractnormalvarsym(p).defaultconstsym)) then
  906. begin
  907. trashintval := trashintvalues[localvartrashing];
  908. case tabstractnormalvarsym(p).initialloc.loc of
  909. LOC_CREGISTER :
  910. {$ifopt q+}
  911. {$define overflowon}
  912. {$q-}
  913. {$endif}
  914. cg.a_load_const_reg(list,reg_cgsize(tabstractnormalvarsym(p).initialloc.register),
  915. trashintval and (aword(1) shl (tcgsize2size[reg_cgsize(tabstractnormalvarsym(p).initialloc.register)] * 8) - 1),
  916. tabstractnormalvarsym(p).initialloc.register);
  917. {$ifdef overflowon}
  918. {$undef overflowon}
  919. {$q+}
  920. {$endif}
  921. LOC_REFERENCE :
  922. begin
  923. if ((tsym(p).typ=localvarsym) and
  924. not(vo_is_funcret in tabstractvarsym(p).varoptions)) or
  925. not is_shortstring(tabstractnormalvarsym(p).vardef) then
  926. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  927. tlocalvarsym(p).getsize)
  928. else
  929. { may be an open string, even if is_open_string() returns }
  930. { false for some helpers in the system unit }
  931. { an open string has at least size 2 }
  932. trash_reference(list,tabstractnormalvarsym(p).initialloc.reference,
  933. 2);
  934. end;
  935. LOC_CMMREGISTER :
  936. ;
  937. LOC_CFPUREGISTER :
  938. ;
  939. else
  940. internalerror(200410124);
  941. end;
  942. end;
  943. end;
  944. { initializes the regvars from staticsymtable with 0 }
  945. procedure initialize_regvars(p:TObject;arg:pointer);
  946. var
  947. href : treference;
  948. begin
  949. if (tsym(p).typ=staticvarsym) then
  950. begin
  951. { Static variables can have the initialloc only set to LOC_CxREGISTER
  952. or LOC_INVALID, for explaination see gen_alloc_symtable (PFV) }
  953. case tstaticvarsym(p).initialloc.loc of
  954. LOC_CREGISTER :
  955. begin
  956. {$ifndef cpu64bitalu}
  957. if (tstaticvarsym(p).initialloc.size in [OS_64,OS_S64]) then
  958. cg64.a_load64_const_reg(TAsmList(arg),0,tstaticvarsym(p).initialloc.register64)
  959. else
  960. {$endif not cpu64bitalu}
  961. cg.a_load_const_reg(TAsmList(arg),reg_cgsize(tstaticvarsym(p).initialloc.register),0,
  962. tstaticvarsym(p).initialloc.register);
  963. end;
  964. LOC_CMMREGISTER :
  965. { clear the whole register }
  966. cg.a_opmm_reg_reg(TAsmList(arg),OP_XOR,reg_cgsize(tstaticvarsym(p).initialloc.register),
  967. tstaticvarsym(p).initialloc.register,
  968. tstaticvarsym(p).initialloc.register,
  969. nil);
  970. LOC_CFPUREGISTER :
  971. begin
  972. { initialize fpu regvar by loading from memory }
  973. reference_reset_symbol(href,
  974. current_asmdata.RefAsmSymbol(tstaticvarsym(p).mangledname), 0,
  975. var_align(tstaticvarsym(p).vardef.alignment));
  976. cg.a_loadfpu_ref_reg(TAsmList(arg), tstaticvarsym(p).initialloc.size,
  977. tstaticvarsym(p).initialloc.size, href, tstaticvarsym(p).initialloc.register);
  978. end;
  979. LOC_INVALID :
  980. ;
  981. else
  982. internalerror(200410124);
  983. end;
  984. end;
  985. end;
  986. { generates the code for initialisation of local data }
  987. procedure initialize_data(p:TObject;arg:pointer);
  988. var
  989. OldAsmList : TAsmList;
  990. hp : tnode;
  991. begin
  992. if (tsym(p).typ in [staticvarsym,localvarsym]) and
  993. ((tabstractvarsym(p).refs>0) or
  994. { managed return symbols must be inited }
  995. ((tsym(p).typ=localvarsym) and (vo_is_funcret in tlocalvarsym(p).varoptions))
  996. ) and
  997. not(vo_is_typed_const in tabstractvarsym(p).varoptions) and
  998. not(vo_is_external in tabstractvarsym(p).varoptions) and
  999. not(is_class(tabstractvarsym(p).vardef)) and
  1000. tabstractvarsym(p).vardef.needs_inittable then
  1001. begin
  1002. OldAsmList:=current_asmdata.CurrAsmList;
  1003. current_asmdata.CurrAsmList:=TAsmList(arg);
  1004. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  1005. firstpass(hp);
  1006. secondpass(hp);
  1007. hp.free;
  1008. current_asmdata.CurrAsmList:=OldAsmList;
  1009. end;
  1010. end;
  1011. procedure finalize_sym(asmlist:TAsmList;sym:tsym);
  1012. var
  1013. hp : tnode;
  1014. OldAsmList : TAsmList;
  1015. begin
  1016. include(current_procinfo.flags,pi_needs_implicit_finally);
  1017. OldAsmList:=current_asmdata.CurrAsmList;
  1018. current_asmdata.CurrAsmList:=asmlist;
  1019. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  1020. firstpass(hp);
  1021. secondpass(hp);
  1022. hp.free;
  1023. current_asmdata.CurrAsmList:=OldAsmList;
  1024. end;
  1025. { generates the code for finalisation of local variables }
  1026. procedure finalize_local_vars(p:TObject;arg:pointer);
  1027. begin
  1028. if (tsym(p).typ=localvarsym) and
  1029. (tlocalvarsym(p).refs>0) and
  1030. not(vo_is_external in tlocalvarsym(p).varoptions) and
  1031. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  1032. not(is_class(tlocalvarsym(p).vardef)) and
  1033. tlocalvarsym(p).vardef.needs_inittable then
  1034. finalize_sym(TAsmList(arg),tsym(p));
  1035. end;
  1036. { generates the code for finalization of static symtable and
  1037. all local (static) typed consts }
  1038. procedure finalize_static_data(p:TObject;arg:pointer);
  1039. var
  1040. i : longint;
  1041. pd : tprocdef;
  1042. begin
  1043. case tsym(p).typ of
  1044. staticvarsym :
  1045. begin
  1046. if (tstaticvarsym(p).refs>0) and
  1047. (tstaticvarsym(p).varspez<>vs_const) and
  1048. not(vo_is_funcret in tstaticvarsym(p).varoptions) and
  1049. not(vo_is_external in tstaticvarsym(p).varoptions) and
  1050. not(is_class(tstaticvarsym(p).vardef)) and
  1051. tstaticvarsym(p).vardef.needs_inittable then
  1052. finalize_sym(TAsmList(arg),tsym(p));
  1053. end;
  1054. procsym :
  1055. begin
  1056. for i:=0 to tprocsym(p).ProcdefList.Count-1 do
  1057. begin
  1058. pd:=tprocdef(tprocsym(p).ProcdefList[i]);
  1059. if assigned(pd.localst) and
  1060. (pd.procsym=tprocsym(p)) and
  1061. (pd.localst.symtabletype<>staticsymtable) then
  1062. pd.localst.SymList.ForEachCall(@finalize_static_data,arg);
  1063. end;
  1064. end;
  1065. end;
  1066. end;
  1067. { generates the code for incrementing the reference count of parameters and
  1068. initialize out parameters }
  1069. procedure init_paras(p:TObject;arg:pointer);
  1070. var
  1071. href : treference;
  1072. tmpreg : tregister;
  1073. list : TAsmList;
  1074. needs_inittable,
  1075. do_trashing : boolean;
  1076. begin
  1077. list:=TAsmList(arg);
  1078. if (tsym(p).typ=paravarsym) then
  1079. begin
  1080. needs_inittable :=
  1081. not is_class(tparavarsym(p).vardef) and
  1082. tparavarsym(p).vardef.needs_inittable;
  1083. do_trashing :=
  1084. (localvartrashing <> -1) and
  1085. (not assigned(tparavarsym(p).defaultconstsym)) and
  1086. not needs_inittable;
  1087. case tparavarsym(p).varspez of
  1088. vs_value :
  1089. if needs_inittable then
  1090. begin
  1091. { variants are already handled by the call to fpc_variant_copy_overwrite if
  1092. they are passed by reference }
  1093. if not((tparavarsym(p).vardef.typ=variantdef) and
  1094. paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vardef,current_procinfo.procdef.proccalloption)) then
  1095. begin
  1096. location_get_data_ref(list,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  1097. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  1098. end;
  1099. end;
  1100. vs_out :
  1101. begin
  1102. if needs_inittable or
  1103. do_trashing then
  1104. begin
  1105. tmpreg:=cg.getaddressregister(list);
  1106. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1107. { we have no idea about the alignment at the callee side,
  1108. and the user also cannot specify "unaligned" here, so
  1109. assume worst case }
  1110. reference_reset_base(href,tmpreg,0,1);
  1111. if do_trashing and
  1112. { needs separate implementation to trash open arrays }
  1113. { since their size is only known at run time }
  1114. not is_special_array(tparavarsym(p).vardef) then
  1115. trash_reference(list,href,tparavarsym(p).vardef.size);
  1116. if needs_inittable then
  1117. cg.g_initialize(list,tparavarsym(p).vardef,href);
  1118. end;
  1119. end;
  1120. else if do_trashing and
  1121. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  1122. begin
  1123. tmpreg:=cg.getaddressregister(list);
  1124. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1125. { should always have standard alignment. If a function is assigned
  1126. to a non-aligned variable, the optimisation to pass this variable
  1127. directly as hidden function result must/cannot be performed
  1128. (see tcallnode.funcret_can_be_reused)
  1129. }
  1130. reference_reset_base(href,tmpreg,0,
  1131. used_align(tparavarsym(p).vardef.alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax));
  1132. { may be an open string, even if is_open_string() returns }
  1133. { false for some helpers in the system unit }
  1134. if not is_shortstring(tparavarsym(p).vardef) then
  1135. trash_reference(list,href,tparavarsym(p).vardef.size)
  1136. else
  1137. { an open string has at least size 2 }
  1138. trash_reference(list,href,2);
  1139. end
  1140. end;
  1141. end;
  1142. end;
  1143. { generates the code for decrementing the reference count of parameters }
  1144. procedure final_paras(p:TObject;arg:pointer);
  1145. var
  1146. list : TAsmList;
  1147. href : treference;
  1148. begin
  1149. if not(tsym(p).typ=paravarsym) then
  1150. exit;
  1151. list:=TAsmList(arg);
  1152. if not is_class(tparavarsym(p).vardef) and
  1153. tparavarsym(p).vardef.needs_inittable then
  1154. begin
  1155. if (tparavarsym(p).varspez=vs_value) then
  1156. begin
  1157. include(current_procinfo.flags,pi_needs_implicit_finally);
  1158. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef),sizeof(pint));
  1159. cg.g_decrrefcount(list,tparavarsym(p).vardef,href);
  1160. end;
  1161. end;
  1162. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  1163. if (tparavarsym(p).varspez=vs_value) and
  1164. (is_open_array(tparavarsym(p).vardef) or
  1165. is_array_of_const(tparavarsym(p).vardef)) then
  1166. begin
  1167. { cdecl functions don't have a high pointer so it is not possible to generate
  1168. a local copy }
  1169. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1170. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  1171. end;
  1172. end;
  1173. { Initialize temp ansi/widestrings,interfaces }
  1174. procedure inittempvariables(list:TAsmList);
  1175. var
  1176. hp : ptemprecord;
  1177. href : treference;
  1178. begin
  1179. hp:=tg.templist;
  1180. while assigned(hp) do
  1181. begin
  1182. if assigned(hp^.def) and
  1183. hp^.def.needs_inittable then
  1184. begin
  1185. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1186. cg.g_initialize(list,hp^.def,href);
  1187. end;
  1188. hp:=hp^.next;
  1189. end;
  1190. end;
  1191. procedure finalizetempvariables(list:TAsmList);
  1192. var
  1193. hp : ptemprecord;
  1194. href : treference;
  1195. begin
  1196. hp:=tg.templist;
  1197. while assigned(hp) do
  1198. begin
  1199. if assigned(hp^.def) and
  1200. hp^.def.needs_inittable then
  1201. begin
  1202. include(current_procinfo.flags,pi_needs_implicit_finally);
  1203. reference_reset_base(href,current_procinfo.framepointer,hp^.pos,sizeof(pint));
  1204. cg.g_finalize(list,hp^.def,href);
  1205. end;
  1206. hp:=hp^.next;
  1207. end;
  1208. end;
  1209. procedure gen_load_return_value(list:TAsmList);
  1210. var
  1211. href : treference;
  1212. ressym : tabstractnormalvarsym;
  1213. resloc,
  1214. restmploc : tlocation;
  1215. hreg : tregister;
  1216. funcretloc : tlocation;
  1217. begin
  1218. { Is the loading needed? }
  1219. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1220. (
  1221. (po_assembler in current_procinfo.procdef.procoptions) and
  1222. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1223. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1224. ) then
  1225. exit;
  1226. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1227. { constructors return self }
  1228. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1229. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  1230. else
  1231. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1232. if (ressym.refs>0) or (ressym.vardef.needs_inittable) then
  1233. begin
  1234. {$ifdef OLDREGVARS}
  1235. case ressym.localloc.loc of
  1236. LOC_CFPUREGISTER,
  1237. LOC_FPUREGISTER:
  1238. begin
  1239. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1240. restmploc.register:=ressym.localloc.register;
  1241. end;
  1242. LOC_CREGISTER,
  1243. LOC_REGISTER:
  1244. begin
  1245. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1246. restmploc.register:=ressym.localloc.register;
  1247. end;
  1248. LOC_MMREGISTER:
  1249. begin
  1250. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1251. restmploc.register:=ressym.localloc.register;
  1252. end;
  1253. LOC_REFERENCE:
  1254. begin
  1255. location_reset_ref(restmploc,LOC_REFERENCE,funcretloc^.size,0);
  1256. restmploc.reference:=ressym.localloc.reference;
  1257. end;
  1258. else
  1259. internalerror(200309184);
  1260. end;
  1261. {$else}
  1262. restmploc:=ressym.localloc;
  1263. {$endif}
  1264. { Here, we return the function result. In most architectures, the value is
  1265. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1266. function returns in a register and the caller receives it in an other one }
  1267. case funcretloc.loc of
  1268. LOC_REGISTER:
  1269. begin
  1270. {$ifdef cpu64bitaddr}
  1271. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_128,OS_S128] then
  1272. begin
  1273. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1274. if resloc.loc<>LOC_REGISTER then
  1275. internalerror(200409141);
  1276. { Load low and high register separate to generate better register
  1277. allocation info }
  1278. if getsupreg(resloc.register)<first_int_imreg then
  1279. begin
  1280. cg.getcpuregister(list,resloc.register);
  1281. end;
  1282. case restmploc.loc of
  1283. LOC_REFERENCE :
  1284. begin
  1285. href:=restmploc.reference;
  1286. if target_info.endian=ENDIAN_BIG then
  1287. inc(href.offset,8);
  1288. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1289. end;
  1290. LOC_CREGISTER :
  1291. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1292. else
  1293. internalerror(200409203);
  1294. end;
  1295. if getsupreg(resloc.registerhi)<first_int_imreg then
  1296. begin
  1297. cg.getcpuregister(list,resloc.registerhi);
  1298. end;
  1299. case restmploc.loc of
  1300. LOC_REFERENCE :
  1301. begin
  1302. href:=restmploc.reference;
  1303. if target_info.endian=ENDIAN_LITTLE then
  1304. inc(href.offset,8);
  1305. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.registerhi);
  1306. end;
  1307. LOC_CREGISTER :
  1308. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.registerhi,resloc.registerhi);
  1309. else
  1310. internalerror(200409204);
  1311. end;
  1312. end
  1313. else
  1314. { this code is for structures etc. being returned in registers and having odd sizes }
  1315. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_64) and
  1316. (restmploc.size<>OS_64) then
  1317. begin
  1318. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1319. if resloc.loc<>LOC_REGISTER then
  1320. internalerror(200409141);
  1321. { Load low and high register separate to generate better register
  1322. allocation info }
  1323. if getsupreg(resloc.register)<first_int_imreg then
  1324. begin
  1325. cg.getcpuregister(list,resloc.register);
  1326. end;
  1327. case restmploc.loc of
  1328. LOC_REFERENCE :
  1329. begin
  1330. href:=restmploc.reference;
  1331. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1332. end;
  1333. LOC_CREGISTER :
  1334. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1335. else
  1336. internalerror(200409203);
  1337. end;
  1338. end
  1339. else
  1340. {$else cpu64bitaddr}
  1341. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1342. begin
  1343. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1344. if resloc.loc<>LOC_REGISTER then
  1345. internalerror(200409141);
  1346. { Load low and high register separate to generate better register
  1347. allocation info }
  1348. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1349. begin
  1350. cg.getcpuregister(list,resloc.register64.reglo);
  1351. end;
  1352. case restmploc.loc of
  1353. LOC_REFERENCE :
  1354. begin
  1355. href:=restmploc.reference;
  1356. if target_info.endian=ENDIAN_BIG then
  1357. inc(href.offset,4);
  1358. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1359. end;
  1360. LOC_CREGISTER :
  1361. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1362. else
  1363. internalerror(200409203);
  1364. end;
  1365. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1366. begin
  1367. cg.getcpuregister(list,resloc.register64.reghi);
  1368. end;
  1369. case restmploc.loc of
  1370. LOC_REFERENCE :
  1371. begin
  1372. href:=restmploc.reference;
  1373. if target_info.endian=ENDIAN_LITTLE then
  1374. inc(href.offset,4);
  1375. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1376. end;
  1377. LOC_CREGISTER :
  1378. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1379. else
  1380. internalerror(200409204);
  1381. end;
  1382. end
  1383. else
  1384. {$endif not cpu64bitaddr}
  1385. { this code is for structures etc. being returned in registers and having odd sizes }
  1386. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_32) and
  1387. not(restmploc.size in [OS_S32,OS_32]) then
  1388. begin
  1389. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1390. if resloc.loc<>LOC_REGISTER then
  1391. internalerror(200409141);
  1392. { Load low and high register separate to generate better register
  1393. allocation info }
  1394. if getsupreg(resloc.register)<first_int_imreg then
  1395. begin
  1396. cg.getcpuregister(list,resloc.register);
  1397. end;
  1398. case restmploc.loc of
  1399. LOC_REFERENCE :
  1400. begin
  1401. href:=restmploc.reference;
  1402. resloc.register:=cg.makeregsize(list,resloc.register,OS_32);
  1403. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register);
  1404. end;
  1405. LOC_CREGISTER :
  1406. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register,resloc.register);
  1407. else
  1408. internalerror(200409203);
  1409. end;
  1410. end
  1411. else
  1412. begin
  1413. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1414. if getsupreg(funcretloc.register)<first_int_imreg then
  1415. begin
  1416. cg.getcpuregister(list,funcretloc.register);
  1417. end;
  1418. { it could be that a structure is passed in memory but the function is expected to
  1419. return a pointer to this memory }
  1420. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1421. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1422. else
  1423. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1424. end;
  1425. end;
  1426. LOC_FPUREGISTER:
  1427. begin
  1428. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1429. begin
  1430. cg.getcpuregister(list,funcretloc.register);
  1431. end;
  1432. { we can't do direct moves between fpu and mm registers }
  1433. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1434. location_force_fpureg(list,restmploc,false);
  1435. cg.a_loadfpu_loc_reg(list,funcretloc.size,restmploc,funcretloc.register);
  1436. end;
  1437. LOC_MMREGISTER:
  1438. begin
  1439. if getsupreg(funcretloc.register)<first_mm_imreg then
  1440. begin
  1441. cg.getcpuregister(list,funcretloc.register);
  1442. end;
  1443. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1444. end;
  1445. LOC_INVALID,
  1446. LOC_REFERENCE:
  1447. ;
  1448. else
  1449. internalerror(200405025);
  1450. end;
  1451. end
  1452. {$ifdef x86}
  1453. else
  1454. begin
  1455. { the caller will pop a value off the cpu stack }
  1456. if (funcretloc.loc = LOC_FPUREGISTER) then
  1457. list.concat(taicpu.op_none(A_FLDZ));
  1458. end;
  1459. {$endif x86}
  1460. end;
  1461. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1462. begin
  1463. case sym.initialloc.loc of
  1464. LOC_CREGISTER:
  1465. begin
  1466. {$ifndef cpu64bitalu}
  1467. if sym.initialloc.size in [OS_64,OS_S64] then
  1468. begin
  1469. sym.initialloc.register64.reglo:=cg.getintregister(list,OS_32);
  1470. sym.initialloc.register64.reghi:=cg.getintregister(list,OS_32);
  1471. end
  1472. else
  1473. {$endif cpu64bitalu}
  1474. sym.initialloc.register:=cg.getintregister(list,sym.initialloc.size);
  1475. end;
  1476. LOC_CFPUREGISTER:
  1477. begin
  1478. sym.initialloc.register:=cg.getfpuregister(list,sym.initialloc.size);
  1479. end;
  1480. LOC_CMMREGISTER:
  1481. begin
  1482. sym.initialloc.register:=cg.getmmregister(list,sym.initialloc.size);
  1483. end;
  1484. end;
  1485. if (pi_has_goto in current_procinfo.flags) then
  1486. begin
  1487. { Allocate register already, to prevent first allocation to be
  1488. inside a loop }
  1489. {$ifndef cpu64bitalu}
  1490. if sym.initialloc.size in [OS_64,OS_S64] then
  1491. begin
  1492. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  1493. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  1494. end
  1495. else
  1496. {$endif not cpu64bitalu}
  1497. cg.a_reg_sync(list,sym.initialloc.register);
  1498. end;
  1499. sym.localloc:=sym.initialloc;
  1500. end;
  1501. procedure gen_load_para_value(list:TAsmList);
  1502. procedure get_para(const paraloc:TCGParaLocation);
  1503. begin
  1504. case paraloc.loc of
  1505. LOC_REGISTER :
  1506. begin
  1507. if getsupreg(paraloc.register)<first_int_imreg then
  1508. cg.getcpuregister(list,paraloc.register);
  1509. end;
  1510. LOC_MMREGISTER :
  1511. begin
  1512. if getsupreg(paraloc.register)<first_mm_imreg then
  1513. cg.getcpuregister(list,paraloc.register);
  1514. end;
  1515. LOC_FPUREGISTER :
  1516. begin
  1517. if getsupreg(paraloc.register)<first_fpu_imreg then
  1518. cg.getcpuregister(list,paraloc.register);
  1519. end;
  1520. end;
  1521. end;
  1522. procedure unget_para(const paraloc:TCGParaLocation);
  1523. begin
  1524. case paraloc.loc of
  1525. LOC_REGISTER :
  1526. begin
  1527. if getsupreg(paraloc.register)<first_int_imreg then
  1528. cg.ungetcpuregister(list,paraloc.register);
  1529. end;
  1530. LOC_MMREGISTER :
  1531. begin
  1532. if getsupreg(paraloc.register)<first_mm_imreg then
  1533. cg.ungetcpuregister(list,paraloc.register);
  1534. end;
  1535. LOC_FPUREGISTER :
  1536. begin
  1537. if getsupreg(paraloc.register)<first_fpu_imreg then
  1538. cg.ungetcpuregister(list,paraloc.register);
  1539. end;
  1540. end;
  1541. end;
  1542. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint; alignment: longint);
  1543. var
  1544. href : treference;
  1545. begin
  1546. case paraloc.loc of
  1547. LOC_REGISTER :
  1548. begin
  1549. {$IFDEF POWERPC64}
  1550. if (paraloc.shiftval <> 0) then
  1551. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1552. {$ENDIF POWERPC64}
  1553. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1554. end;
  1555. LOC_MMREGISTER :
  1556. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1557. LOC_FPUREGISTER :
  1558. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1559. LOC_REFERENCE :
  1560. begin
  1561. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,alignment);
  1562. { use concatcopy, because it can also be a float which fails when
  1563. load_ref_ref is used. Don't copy data when the references are equal }
  1564. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1565. cg.g_concatcopy(list,href,ref,sizeleft);
  1566. end;
  1567. else
  1568. internalerror(2002081302);
  1569. end;
  1570. end;
  1571. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister; alignment: longint);
  1572. var
  1573. href : treference;
  1574. begin
  1575. case paraloc.loc of
  1576. LOC_REGISTER :
  1577. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1578. LOC_MMREGISTER :
  1579. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1580. LOC_FPUREGISTER :
  1581. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1582. LOC_REFERENCE :
  1583. begin
  1584. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset,alignment);
  1585. case getregtype(reg) of
  1586. R_INTREGISTER :
  1587. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1588. R_FPUREGISTER :
  1589. cg.a_loadfpu_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1590. R_MMREGISTER :
  1591. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1592. else
  1593. internalerror(2004101012);
  1594. end;
  1595. end;
  1596. else
  1597. internalerror(2002081302);
  1598. end;
  1599. end;
  1600. var
  1601. i : longint;
  1602. currpara : tparavarsym;
  1603. paraloc : pcgparalocation;
  1604. href : treference;
  1605. sizeleft : aint;
  1606. {$if defined(sparc) or defined(arm)}
  1607. tempref : treference;
  1608. {$endif sparc}
  1609. begin
  1610. if (po_assembler in current_procinfo.procdef.procoptions) then
  1611. exit;
  1612. { Allocate registers used by parameters }
  1613. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1614. begin
  1615. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1616. paraloc:=currpara.paraloc[calleeside].location;
  1617. while assigned(paraloc) do
  1618. begin
  1619. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1620. get_para(paraloc^);
  1621. paraloc:=paraloc^.next;
  1622. end;
  1623. end;
  1624. { Copy parameters to local references/registers }
  1625. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1626. begin
  1627. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1628. paraloc:=currpara.paraloc[calleeside].location;
  1629. { skip e.g. empty records }
  1630. if not assigned(paraloc) then
  1631. internalerror(200408203);
  1632. if (paraloc^.loc = LOC_VOID) then
  1633. continue;
  1634. case currpara.initialloc.loc of
  1635. LOC_REFERENCE :
  1636. begin
  1637. { If the parameter location is reused we don't need to copy
  1638. anything }
  1639. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1640. begin
  1641. href:=currpara.initialloc.reference;
  1642. sizeleft:=currpara.paraloc[calleeside].intsize;
  1643. while assigned(paraloc) do
  1644. begin
  1645. if (paraloc^.size=OS_NO) then
  1646. begin
  1647. { Can only be a reference that contains the rest
  1648. of the parameter }
  1649. if (paraloc^.loc<>LOC_REFERENCE) or
  1650. assigned(paraloc^.next) then
  1651. internalerror(2005013010);
  1652. gen_load_ref(paraloc^,href,sizeleft,currpara.initialloc.reference.alignment);
  1653. inc(href.offset,sizeleft);
  1654. sizeleft:=0;
  1655. end
  1656. else
  1657. begin
  1658. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size],currpara.initialloc.reference.alignment);
  1659. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1660. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1661. end;
  1662. unget_para(paraloc^);
  1663. paraloc:=paraloc^.next;
  1664. end;
  1665. end;
  1666. end;
  1667. LOC_CREGISTER :
  1668. begin
  1669. {$ifndef cpu64bitalu}
  1670. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1671. is_64bit(currpara.vardef) then
  1672. begin
  1673. case paraloc^.loc of
  1674. LOC_REGISTER:
  1675. begin
  1676. if not assigned(paraloc^.next) then
  1677. internalerror(200410104);
  1678. if (target_info.endian=ENDIAN_BIG) then
  1679. begin
  1680. { paraloc^ -> high
  1681. paraloc^.next -> low }
  1682. unget_para(paraloc^);
  1683. gen_alloc_regvar(list,currpara);
  1684. { reg->reg, alignment is irrelevant }
  1685. gen_load_reg(paraloc^,currpara.initialloc.register64.reghi,4);
  1686. unget_para(paraloc^.next^);
  1687. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reglo,4);
  1688. end
  1689. else
  1690. begin
  1691. { paraloc^ -> low
  1692. paraloc^.next -> high }
  1693. unget_para(paraloc^);
  1694. gen_alloc_regvar(list,currpara);
  1695. gen_load_reg(paraloc^,currpara.initialloc.register64.reglo,4);
  1696. unget_para(paraloc^.next^);
  1697. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reghi,4);
  1698. end;
  1699. end;
  1700. LOC_REFERENCE:
  1701. begin
  1702. gen_alloc_regvar(list,currpara);
  1703. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset,currpara.paraloc[calleeside].alignment);
  1704. cg64.a_load64_ref_reg(list,href,currpara.initialloc.register64);
  1705. unget_para(paraloc^);
  1706. end;
  1707. else
  1708. internalerror(2005101501);
  1709. end
  1710. end
  1711. else
  1712. {$endif not cpu64bitalu}
  1713. begin
  1714. if assigned(paraloc^.next) then
  1715. internalerror(200410105);
  1716. unget_para(paraloc^);
  1717. gen_alloc_regvar(list,currpara);
  1718. gen_load_reg(paraloc^,currpara.initialloc.register,sizeof(aint));
  1719. end;
  1720. end;
  1721. LOC_CFPUREGISTER :
  1722. begin
  1723. {$if defined(sparc) or defined(arm)}
  1724. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1725. we need a temp }
  1726. sizeleft := TCGSize2Size[currpara.initialloc.size];
  1727. tg.GetTemp(list,sizeleft,sizeleft,tt_normal,tempref);
  1728. href:=tempref;
  1729. while assigned(paraloc) do
  1730. begin
  1731. unget_para(paraloc^);
  1732. gen_load_ref(paraloc^,href,sizeleft,currpara.initialloc.reference.alignment);
  1733. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1734. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1735. paraloc:=paraloc^.next;
  1736. end;
  1737. gen_alloc_regvar(list,currpara);
  1738. cg.a_loadfpu_ref_reg(list,currpara.initialloc.size,currpara.initialloc.size,tempref,currpara.initialloc.register);
  1739. tg.UnGetTemp(list,tempref);
  1740. {$else sparc}
  1741. unget_para(paraloc^);
  1742. gen_alloc_regvar(list,currpara);
  1743. { from register to register -> alignment is irrelevant }
  1744. gen_load_reg(paraloc^,currpara.initialloc.register,0);
  1745. if assigned(paraloc^.next) then
  1746. internalerror(200410109);
  1747. {$endif sparc}
  1748. end;
  1749. LOC_CMMREGISTER :
  1750. begin
  1751. unget_para(paraloc^);
  1752. gen_alloc_regvar(list,currpara);
  1753. { from register to register -> alignment is irrelevant }
  1754. gen_load_reg(paraloc^,currpara.initialloc.register,0);
  1755. { data could come in two memory locations, for now
  1756. we simply ignore the sanity check (FK)
  1757. if assigned(paraloc^.next) then
  1758. internalerror(200410108);
  1759. }
  1760. end;
  1761. end;
  1762. end;
  1763. { generate copies of call by value parameters, must be done before
  1764. the initialization and body is parsed because the refcounts are
  1765. incremented using the local copies }
  1766. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1767. {$ifdef powerpc}
  1768. { unget the register that contains the stack pointer before the procedure entry, }
  1769. { which is used to access the parameters in their original callee-side location }
  1770. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1771. cg.a_reg_dealloc(list,NR_R12);
  1772. {$endif powerpc}
  1773. {$ifdef powerpc64}
  1774. { unget the register that contains the stack pointer before the procedure entry, }
  1775. { which is used to access the parameters in their original callee-side location }
  1776. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1777. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1778. {$endif powerpc64}
  1779. end;
  1780. procedure gen_initialize_code(list:TAsmList);
  1781. begin
  1782. { initialize local data like ansistrings }
  1783. case current_procinfo.procdef.proctypeoption of
  1784. potype_unitinit:
  1785. begin
  1786. { this is also used for initialization of variables in a
  1787. program which does not have a globalsymtable }
  1788. if assigned(current_module.globalsymtable) then
  1789. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1790. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1791. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1792. end;
  1793. { units have seperate code for initilization and finalization }
  1794. potype_unitfinalize: ;
  1795. { program init/final is generated in separate procedure }
  1796. potype_proginit:
  1797. begin
  1798. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1799. end;
  1800. else
  1801. begin
  1802. if (localvartrashing <> -1) and
  1803. not(po_assembler in current_procinfo.procdef.procoptions) then
  1804. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1805. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1806. end;
  1807. end;
  1808. { initialisizes temp. ansi/wide string data }
  1809. inittempvariables(list);
  1810. { initialize ansi/widesstring para's }
  1811. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1812. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1813. {$ifdef OLDREGVARS}
  1814. load_regvars(list,nil);
  1815. {$endif OLDREGVARS}
  1816. end;
  1817. procedure gen_finalize_code(list:TAsmList);
  1818. begin
  1819. {$ifdef OLDREGVARS}
  1820. cleanup_regvars(list);
  1821. {$endif OLDREGVARS}
  1822. { finalize temporary data }
  1823. finalizetempvariables(list);
  1824. { finalize local data like ansistrings}
  1825. case current_procinfo.procdef.proctypeoption of
  1826. potype_unitfinalize:
  1827. begin
  1828. { this is also used for initialization of variables in a
  1829. program which does not have a globalsymtable }
  1830. if assigned(current_module.globalsymtable) then
  1831. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1832. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1833. end;
  1834. { units/progs have separate code for initialization and finalization }
  1835. potype_unitinit: ;
  1836. { program init/final is generated in separate procedure }
  1837. potype_proginit: ;
  1838. else
  1839. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1840. end;
  1841. { finalize paras data }
  1842. if assigned(current_procinfo.procdef.parast) and
  1843. not(po_assembler in current_procinfo.procdef.procoptions) then
  1844. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1845. end;
  1846. procedure gen_entry_code(list:TAsmList);
  1847. begin
  1848. { the actual profile code can clobber some registers,
  1849. therefore if the context must be saved, do it before
  1850. the actual call to the profile code
  1851. }
  1852. if (cs_profile in current_settings.moduleswitches) and
  1853. not(po_assembler in current_procinfo.procdef.procoptions) then
  1854. begin
  1855. { non-win32 can call mcout even in main }
  1856. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1857. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1858. begin
  1859. cg.g_profilecode(list);
  1860. end;
  1861. end;
  1862. { call startup helpers from main program }
  1863. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1864. begin
  1865. { initialize units }
  1866. cg.allocallcpuregisters(list);
  1867. if not(current_module.islibrary) then
  1868. cg.a_call_name(list,'FPC_INITIALIZEUNITS',false)
  1869. else
  1870. cg.a_call_name(list,'FPC_LIBINITIALIZEUNITS',false);
  1871. cg.deallocallcpuregisters(list);
  1872. end;
  1873. list.concat(Tai_force_line.Create);
  1874. {$ifdef OLDREGVARS}
  1875. load_regvars(list,nil);
  1876. {$endif OLDREGVARS}
  1877. end;
  1878. procedure gen_exit_code(list:TAsmList);
  1879. begin
  1880. { call __EXIT for main program }
  1881. if (not DLLsource) and
  1882. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1883. cg.a_call_name(list,'FPC_DO_EXIT',false);
  1884. end;
  1885. {****************************************************************************
  1886. Entry/Exit
  1887. ****************************************************************************}
  1888. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1889. var
  1890. item : TCmdStrListItem;
  1891. begin
  1892. result:=true;
  1893. if pd.mangledname=s then
  1894. exit;
  1895. item := TCmdStrListItem(pd.aliasnames.first);
  1896. while assigned(item) do
  1897. begin
  1898. if item.str=s then
  1899. exit;
  1900. item := TCmdStrListItem(item.next);
  1901. end;
  1902. result:=false;
  1903. end;
  1904. procedure alloc_proc_symbol(pd: tprocdef);
  1905. var
  1906. item : TCmdStrListItem;
  1907. begin
  1908. item := TCmdStrListItem(pd.aliasnames.first);
  1909. while assigned(item) do
  1910. begin
  1911. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1912. item := TCmdStrListItem(item.next);
  1913. end;
  1914. end;
  1915. procedure gen_proc_symbol(list:TAsmList);
  1916. var
  1917. item,
  1918. previtem : TCmdStrListItem;
  1919. begin
  1920. previtem:=nil;
  1921. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1922. while assigned(item) do
  1923. begin
  1924. { "double link" all procedure entry symbols via .reference }
  1925. { directives on darwin, because otherwise the linker }
  1926. { sometimes strips the procedure if only on of the symbols }
  1927. { is referenced }
  1928. if assigned(previtem) and
  1929. (target_info.system in systems_darwin) then
  1930. list.concat(tai_directive.create(asd_reference,item.str));
  1931. if (cs_profile in current_settings.moduleswitches) or
  1932. (po_global in current_procinfo.procdef.procoptions) then
  1933. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1934. else
  1935. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1936. if assigned(previtem) and
  1937. (target_info.system in systems_darwin) then
  1938. list.concat(tai_directive.create(asd_reference,previtem.str));
  1939. if tf_use_function_relative_addresses in target_info.flags then
  1940. list.concat(Tai_function_name.create(item.str));
  1941. previtem:=item;
  1942. item := TCmdStrListItem(item.next);
  1943. end;
  1944. current_procinfo.procdef.procstarttai:=tai(list.last);
  1945. end;
  1946. procedure gen_proc_symbol_end(list:TAsmList);
  1947. begin
  1948. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1949. current_procinfo.procdef.procendtai:=tai(list.last);
  1950. if (current_module.islibrary) then
  1951. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1952. exportlib.setinitname(list,current_procinfo.procdef.mangledname);
  1953. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1954. begin
  1955. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1956. not(current_module.islibrary) then
  1957. begin
  1958. list.concat(tai_section.create(sec_code,'',4));
  1959. list.concat(tai_symbol.createname_global(
  1960. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1961. { keep argc, argv and envp properly on the stack }
  1962. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1963. end;
  1964. end;
  1965. end;
  1966. procedure gen_proc_entry_code(list:TAsmList);
  1967. var
  1968. hitemp,
  1969. lotemp : longint;
  1970. begin
  1971. { generate call frame marker for dwarf call frame info }
  1972. current_asmdata.asmcfi.start_frame(list);
  1973. { All temps are know, write offsets used for information }
  1974. if (cs_asm_source in current_settings.globalswitches) then
  1975. begin
  1976. if tg.direction>0 then
  1977. begin
  1978. lotemp:=current_procinfo.tempstart;
  1979. hitemp:=tg.lasttemp;
  1980. end
  1981. else
  1982. begin
  1983. lotemp:=tg.lasttemp;
  1984. hitemp:=current_procinfo.tempstart;
  1985. end;
  1986. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1987. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1988. end;
  1989. { generate target specific proc entry code }
  1990. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1991. end;
  1992. procedure gen_proc_exit_code(list:TAsmList);
  1993. var
  1994. parasize : longint;
  1995. begin
  1996. { c style clearstack does not need to remove parameters from the stack, only the
  1997. return value when it was pushed by arguments }
  1998. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1999. begin
  2000. parasize:=0;
  2001. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  2002. inc(parasize,sizeof(pint));
  2003. end
  2004. else
  2005. parasize:=current_procinfo.para_stack_size;
  2006. { generate target specific proc exit code }
  2007. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  2008. { release return registers, needed for optimizer }
  2009. if not is_void(current_procinfo.procdef.returndef) then
  2010. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  2011. { end of frame marker for call frame info }
  2012. current_asmdata.asmcfi.end_frame(list);
  2013. end;
  2014. procedure gen_stack_check_size_para(list:TAsmList);
  2015. var
  2016. paraloc1 : tcgpara;
  2017. begin
  2018. paraloc1.init;
  2019. paramanager.getintparaloc(pocall_default,1,paraloc1);
  2020. paramanager.allocparaloc(list,paraloc1);
  2021. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  2022. paramanager.freeparaloc(list,paraloc1);
  2023. paraloc1.done;
  2024. end;
  2025. procedure gen_stack_check_call(list:TAsmList);
  2026. var
  2027. paraloc1 : tcgpara;
  2028. begin
  2029. paraloc1.init;
  2030. { Also alloc the register needed for the parameter }
  2031. paramanager.getintparaloc(pocall_default,1,paraloc1);
  2032. paramanager.allocparaloc(list,paraloc1);
  2033. paramanager.freeparaloc(list,paraloc1);
  2034. { Call the helper }
  2035. cg.allocallcpuregisters(list);
  2036. cg.a_call_name(list,'FPC_STACKCHECK',false);
  2037. cg.deallocallcpuregisters(list);
  2038. paraloc1.done;
  2039. end;
  2040. procedure gen_save_used_regs(list:TAsmList);
  2041. begin
  2042. { Pure assembler routines need to save the registers themselves }
  2043. if (po_assembler in current_procinfo.procdef.procoptions) then
  2044. exit;
  2045. { oldfpccall expects all registers to be destroyed }
  2046. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2047. cg.g_save_registers(list);
  2048. end;
  2049. procedure gen_restore_used_regs(list:TAsmList);
  2050. begin
  2051. { Pure assembler routines need to save the registers themselves }
  2052. if (po_assembler in current_procinfo.procdef.procoptions) then
  2053. exit;
  2054. { oldfpccall expects all registers to be destroyed }
  2055. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2056. cg.g_restore_registers(list);
  2057. end;
  2058. {****************************************************************************
  2059. External handling
  2060. ****************************************************************************}
  2061. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  2062. begin
  2063. { add the procedure to the al_procedures }
  2064. maybe_new_object_file(list);
  2065. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  2066. list.concat(Tai_align.create(current_settings.alignment.procalign));
  2067. if (po_global in pd.procoptions) then
  2068. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  2069. else
  2070. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  2071. cg.g_external_wrapper(list,pd,externalname);
  2072. end;
  2073. {****************************************************************************
  2074. Const Data
  2075. ****************************************************************************}
  2076. procedure insertbssdata(sym : tstaticvarsym);
  2077. var
  2078. l : aint;
  2079. varalign : shortint;
  2080. storefilepos : tfileposinfo;
  2081. list : TAsmList;
  2082. sectype : TAsmSectiontype;
  2083. begin
  2084. storefilepos:=current_filepos;
  2085. current_filepos:=sym.fileinfo;
  2086. l:=sym.getsize;
  2087. varalign:=sym.vardef.alignment;
  2088. if (varalign=0) then
  2089. varalign:=var_align_size(l)
  2090. else
  2091. varalign:=var_align(varalign);
  2092. if tf_section_threadvars in target_info.flags then
  2093. begin
  2094. if (vo_is_thread_var in sym.varoptions) then
  2095. begin
  2096. list:=current_asmdata.asmlists[al_threadvars];
  2097. sectype:=sec_threadvar;
  2098. end
  2099. else
  2100. begin
  2101. list:=current_asmdata.asmlists[al_globals];
  2102. sectype:=sec_bss;
  2103. end;
  2104. end
  2105. else
  2106. begin
  2107. if (vo_is_thread_var in sym.varoptions) then
  2108. begin
  2109. inc(l,sizeof(pint));
  2110. { it doesn't help to set a higher alignment, as }
  2111. { the first sizeof(pint) bytes field will offset }
  2112. { everything anyway }
  2113. varalign:=sizeof(pint);
  2114. end;
  2115. list:=current_asmdata.asmlists[al_globals];
  2116. sectype:=sec_bss;
  2117. end;
  2118. maybe_new_object_file(list);
  2119. new_section(list,sectype,lower(sym.mangledname),varalign);
  2120. if (sym.owner.symtabletype=globalsymtable) or
  2121. create_smartlink or
  2122. DLLSource or
  2123. (assigned(current_procinfo) and
  2124. (po_inline in current_procinfo.procdef.procoptions)) or
  2125. (vo_is_public in sym.varoptions) then
  2126. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  2127. else
  2128. list.concat(Tai_datablock.create(sym.mangledname,l));
  2129. current_filepos:=storefilepos;
  2130. end;
  2131. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  2132. procedure setlocalloc(vs:tabstractnormalvarsym);
  2133. begin
  2134. if cs_asm_source in current_settings.globalswitches then
  2135. begin
  2136. case vs.initialloc.loc of
  2137. LOC_REFERENCE :
  2138. begin
  2139. if not assigned(vs.initialloc.reference.symbol) then
  2140. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  2141. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  2142. end;
  2143. end;
  2144. end;
  2145. vs.localloc:=vs.initialloc;
  2146. end;
  2147. var
  2148. i : longint;
  2149. sym : tsym;
  2150. vs : tabstractnormalvarsym;
  2151. isaddr : boolean;
  2152. begin
  2153. for i:=0 to st.SymList.Count-1 do
  2154. begin
  2155. sym:=tsym(st.SymList[i]);
  2156. case sym.typ of
  2157. staticvarsym :
  2158. begin
  2159. vs:=tabstractnormalvarsym(sym);
  2160. { The code in loadnode.pass_generatecode will create the
  2161. LOC_REFERENCE instead for all none register variables. This is
  2162. required because we can't store an asmsymbol in the localloc because
  2163. the asmsymbol is invalid after an unit is compiled. This gives
  2164. problems when this procedure is inlined in an other unit (PFV) }
  2165. if vs.is_regvar(false) then
  2166. begin
  2167. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2168. vs.initialloc.size:=def_cgsize(vs.vardef);
  2169. gen_alloc_regvar(list,vs);
  2170. setlocalloc(vs);
  2171. end;
  2172. end;
  2173. paravarsym :
  2174. begin
  2175. vs:=tabstractnormalvarsym(sym);
  2176. { Parameters passed to assembler procedures need to be kept
  2177. in the original location }
  2178. if (po_assembler in current_procinfo.procdef.procoptions) then
  2179. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  2180. else
  2181. begin
  2182. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  2183. if isaddr then
  2184. vs.initialloc.size:=OS_ADDR
  2185. else
  2186. vs.initialloc.size:=def_cgsize(vs.vardef);
  2187. if vs.is_regvar(isaddr) then
  2188. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  2189. else
  2190. begin
  2191. vs.initialloc.loc:=LOC_REFERENCE;
  2192. { Reuse the parameter location for values to are at a single location on the stack }
  2193. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  2194. begin
  2195. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  2196. tparavarsym(sym).paraloc[calleeside].location^.reference.offset,tparavarsym(sym).paraloc[calleeside].alignment);
  2197. end
  2198. else
  2199. begin
  2200. if isaddr then
  2201. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  2202. else
  2203. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  2204. end;
  2205. end;
  2206. end;
  2207. setlocalloc(vs);
  2208. end;
  2209. localvarsym :
  2210. begin
  2211. vs:=tabstractnormalvarsym(sym);
  2212. vs.initialloc.size:=def_cgsize(vs.vardef);
  2213. if (m_delphi in current_settings.modeswitches) and
  2214. (po_assembler in current_procinfo.procdef.procoptions) and
  2215. (vo_is_funcret in vs.varoptions) and
  2216. (vs.refs=0) then
  2217. begin
  2218. { not referenced, so don't allocate. Use dummy to }
  2219. { avoid ie's later on because of LOC_INVALID }
  2220. vs.initialloc.loc:=LOC_REGISTER;
  2221. vs.initialloc.size:=OS_INT;
  2222. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  2223. end
  2224. else if vs.is_regvar(false) then
  2225. begin
  2226. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2227. gen_alloc_regvar(list,vs);
  2228. end
  2229. else
  2230. begin
  2231. vs.initialloc.loc:=LOC_REFERENCE;
  2232. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  2233. end;
  2234. setlocalloc(vs);
  2235. end;
  2236. end;
  2237. end;
  2238. end;
  2239. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  2240. begin
  2241. case location.loc of
  2242. LOC_CREGISTER:
  2243. {$ifndef cpu64bitalu}
  2244. if location.size in [OS_64,OS_S64] then
  2245. begin
  2246. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  2247. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  2248. end
  2249. else
  2250. {$endif not cpu64bitalu}
  2251. rv.intregvars.addnodup(getsupreg(location.register));
  2252. LOC_CFPUREGISTER:
  2253. rv.fpuregvars.addnodup(getsupreg(location.register));
  2254. LOC_CMMREGISTER:
  2255. rv.mmregvars.addnodup(getsupreg(location.register));
  2256. end;
  2257. end;
  2258. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  2259. var
  2260. rv: pusedregvars absolute arg;
  2261. begin
  2262. case (n.nodetype) of
  2263. temprefn:
  2264. { We only have to synchronise a tempnode before a loop if it is }
  2265. { not created inside the loop, and only synchronise after the }
  2266. { loop if it's not destroyed inside the loop. If it's created }
  2267. { before the loop and not yet destroyed, then before the loop }
  2268. { is secondpassed tempinfo^.valid will be true, and we get the }
  2269. { correct registers. If it's not destroyed inside the loop, }
  2270. { then after the loop has been secondpassed tempinfo^.valid }
  2271. { be true and we also get the right registers. In other cases, }
  2272. { tempinfo^.valid will be false and so we do not add }
  2273. { unnecessary registers. This way, we don't have to look at }
  2274. { tempcreate and tempdestroy nodes to get this info (JM) }
  2275. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  2276. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2277. loadn:
  2278. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2279. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2280. vecn:
  2281. { range checks sometimes need the high parameter }
  2282. if (cs_check_range in current_settings.localswitches) and
  2283. (is_open_array(tvecnode(n).left.resultdef) or
  2284. is_array_of_const(tvecnode(n).left.resultdef)) and
  2285. not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2286. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  2287. end;
  2288. result := fen_true;
  2289. end;
  2290. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2291. begin
  2292. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2293. end;
  2294. (*
  2295. See comments at declaration of pusedregvarscommon
  2296. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2297. var
  2298. rv: pusedregvarscommon absolute arg;
  2299. begin
  2300. if (n.nodetype = loadn) and
  2301. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2302. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2303. case loc of
  2304. LOC_CREGISTER:
  2305. { if not yet encountered in this node tree }
  2306. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2307. { but nevertheless already encountered somewhere }
  2308. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2309. { then it's a regvar used in two or more node trees }
  2310. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2311. LOC_CFPUREGISTER:
  2312. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2313. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2314. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2315. LOC_CMMREGISTER:
  2316. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2317. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2318. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2319. end;
  2320. result := fen_true;
  2321. end;
  2322. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2323. begin
  2324. rv.myregvars.intregvars.clear;
  2325. rv.myregvars.fpuregvars.clear;
  2326. rv.myregvars.mmregvars.clear;
  2327. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2328. end;
  2329. *)
  2330. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2331. var
  2332. count: longint;
  2333. begin
  2334. for count := 1 to rv.intregvars.length do
  2335. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2336. for count := 1 to rv.fpuregvars.length do
  2337. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2338. for count := 1 to rv.mmregvars.length do
  2339. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2340. end;
  2341. {*****************************************************************************
  2342. SSA support
  2343. *****************************************************************************}
  2344. type
  2345. preplaceregrec = ^treplaceregrec;
  2346. treplaceregrec = record
  2347. old, new: tregister;
  2348. {$ifndef cpu64bitalu}
  2349. oldhi, newhi: tregister;
  2350. {$endif not cpu64bitalu}
  2351. ressym: tsym;
  2352. end;
  2353. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2354. var
  2355. rr: preplaceregrec absolute para;
  2356. begin
  2357. result := fen_false;
  2358. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  2359. exit;
  2360. case n.nodetype of
  2361. loadn:
  2362. begin
  2363. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2364. not assigned(tloadnode(n).left) and
  2365. (tloadnode(n).symtableentry <> rr^.ressym) and
  2366. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2367. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2368. begin
  2369. {$ifndef cpu64bitalu}
  2370. { it's possible a 64 bit location was shifted and/xor typecasted }
  2371. { in a 32 bit value, so only 1 register was left in the location }
  2372. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2373. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2374. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2375. else
  2376. exit;
  2377. {$endif not cpu64bitalu}
  2378. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2379. result := fen_norecurse_true;
  2380. end;
  2381. end;
  2382. temprefn:
  2383. begin
  2384. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  2385. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2386. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2387. begin
  2388. {$ifndef cpu64bitalu}
  2389. { it's possible a 64 bit location was shifted and/xor typecasted }
  2390. { in a 32 bit value, so only 1 register was left in the location }
  2391. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2392. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2393. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2394. else
  2395. exit;
  2396. {$endif not cpu64bitalu}
  2397. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2398. result := fen_norecurse_true;
  2399. end;
  2400. end;
  2401. { optimize the searching a bit }
  2402. derefn,addrn,
  2403. calln,inlinen,casen,
  2404. addn,subn,muln,
  2405. andn,orn,xorn,
  2406. ltn,lten,gtn,gten,equaln,unequaln,
  2407. slashn,divn,shrn,shln,notn,
  2408. inn,
  2409. asn,isn:
  2410. result := fen_norecurse_false;
  2411. end;
  2412. end;
  2413. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  2414. var
  2415. rr: treplaceregrec;
  2416. begin
  2417. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2418. ([fc_inflowcontrol,fc_gotolabel,fc_lefthandled] * flowcontrol <> []) then
  2419. exit;
  2420. rr.old := n.location.register;
  2421. rr.ressym := nil;
  2422. {$ifndef cpu64bitalu}
  2423. rr.oldhi := NR_NO;
  2424. {$endif not cpu64bitalu}
  2425. case n.location.loc of
  2426. LOC_CREGISTER:
  2427. begin
  2428. {$ifndef cpu64bitalu}
  2429. if (n.location.size in [OS_64,OS_S64]) then
  2430. begin
  2431. rr.oldhi := n.location.register64.reghi;
  2432. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2433. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2434. end
  2435. else
  2436. {$endif not cpu64bitalu}
  2437. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2438. end;
  2439. LOC_CFPUREGISTER:
  2440. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2441. {$ifdef SUPPORT_MMX}
  2442. LOC_CMMXREGISTER:
  2443. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2444. {$endif SUPPORT_MMX}
  2445. LOC_CMMREGISTER:
  2446. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2447. else
  2448. exit;
  2449. end;
  2450. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  2451. assigned(current_procinfo.procdef.funcretsym) and
  2452. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2453. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2454. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  2455. else
  2456. rr.ressym:=current_procinfo.procdef.funcretsym;
  2457. if not foreachnodestatic(n,@doreplace,@rr) then
  2458. exit;
  2459. if reload then
  2460. case n.location.loc of
  2461. LOC_CREGISTER:
  2462. begin
  2463. {$ifndef cpu64bitalu}
  2464. if (n.location.size in [OS_64,OS_S64]) then
  2465. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  2466. else
  2467. {$endif not cpu64bitalu}
  2468. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2469. end;
  2470. LOC_CFPUREGISTER:
  2471. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2472. {$ifdef SUPPORT_MMX}
  2473. LOC_CMMXREGISTER:
  2474. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  2475. {$endif SUPPORT_MMX}
  2476. LOC_CMMREGISTER:
  2477. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  2478. else
  2479. internalerror(2006090920);
  2480. end;
  2481. { now that we've change the loadn/temp, also change the node result location }
  2482. {$ifndef cpu64bitalu}
  2483. if (n.location.size in [OS_64,OS_S64]) then
  2484. begin
  2485. n.location.register64.reglo := rr.new;
  2486. n.location.register64.reghi := rr.newhi;
  2487. end
  2488. else
  2489. {$endif not cpu64bitalu}
  2490. n.location.register := rr.new;
  2491. end;
  2492. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  2493. var
  2494. i : longint;
  2495. sym : tsym;
  2496. begin
  2497. for i:=0 to st.SymList.Count-1 do
  2498. begin
  2499. sym:=tsym(st.SymList[i]);
  2500. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  2501. begin
  2502. with tabstractnormalvarsym(sym) do
  2503. begin
  2504. { Note: We need to keep the data available in memory
  2505. for the sub procedures that can access local data
  2506. in the parent procedures }
  2507. case localloc.loc of
  2508. LOC_CREGISTER :
  2509. if (pi_has_goto in current_procinfo.flags) then
  2510. {$ifndef cpu64bitalu}
  2511. if def_cgsize(vardef) in [OS_64,OS_S64] then
  2512. begin
  2513. cg.a_reg_sync(list,localloc.register64.reglo);
  2514. cg.a_reg_sync(list,localloc.register64.reghi);
  2515. end
  2516. else
  2517. {$endif not cpu64bitalu}
  2518. cg.a_reg_sync(list,localloc.register);
  2519. LOC_CFPUREGISTER,
  2520. LOC_CMMREGISTER:
  2521. if (pi_has_goto in current_procinfo.flags) then
  2522. cg.a_reg_sync(list,localloc.register);
  2523. LOC_REFERENCE :
  2524. begin
  2525. if typ in [localvarsym,paravarsym] then
  2526. tg.Ungetlocal(list,localloc.reference);
  2527. end;
  2528. end;
  2529. end;
  2530. end;
  2531. end;
  2532. end;
  2533. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2534. var
  2535. i,j : longint;
  2536. tmps : string;
  2537. pd : TProcdef;
  2538. ImplIntf : TImplementedInterface;
  2539. begin
  2540. for i:=0 to _class.ImplementedInterfaces.count-1 do
  2541. begin
  2542. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  2543. if (ImplIntf=ImplIntf.VtblImplIntf) and
  2544. assigned(ImplIntf.ProcDefs) then
  2545. begin
  2546. maybe_new_object_file(list);
  2547. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  2548. begin
  2549. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  2550. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2551. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  2552. { create wrapper code }
  2553. new_section(list,sec_code,tmps,0);
  2554. cg.init_register_allocators;
  2555. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  2556. cg.done_register_allocators;
  2557. end;
  2558. end;
  2559. end;
  2560. end;
  2561. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  2562. var
  2563. i : longint;
  2564. def : tdef;
  2565. begin
  2566. for i:=0 to st.DefList.Count-1 do
  2567. begin
  2568. def:=tdef(st.DefList[i]);
  2569. if is_class(def) then
  2570. gen_intf_wrapper(list,tobjectdef(def));
  2571. end;
  2572. end;
  2573. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2574. var
  2575. href : treference;
  2576. begin
  2577. if is_object(objdef) then
  2578. begin
  2579. case selfloc.loc of
  2580. LOC_CREFERENCE,
  2581. LOC_REFERENCE:
  2582. begin
  2583. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2584. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2585. end;
  2586. else
  2587. internalerror(200305056);
  2588. end;
  2589. end
  2590. else
  2591. begin
  2592. case selfloc.loc of
  2593. LOC_REGISTER:
  2594. begin
  2595. {$ifdef cpu_uses_separate_address_registers}
  2596. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2597. begin
  2598. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2599. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2600. end
  2601. else
  2602. {$endif cpu_uses_separate_address_registers}
  2603. reference_reset_base(href,selfloc.register,objdef.vmt_offset,sizeof(pint));
  2604. end;
  2605. LOC_CREGISTER,
  2606. LOC_CREFERENCE,
  2607. LOC_REFERENCE:
  2608. begin
  2609. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset,sizeof(pint));
  2610. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2611. end;
  2612. else
  2613. internalerror(200305057);
  2614. end;
  2615. end;
  2616. vmtreg:=cg.getaddressregister(list);
  2617. cg.g_maybe_testself(list,href.base);
  2618. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2619. { test validity of VMT }
  2620. if not(is_interface(objdef)) and
  2621. not(is_cppclass(objdef)) then
  2622. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2623. end;
  2624. function getprocalign : shortint;
  2625. begin
  2626. { gprof uses 16 byte granularity }
  2627. if (cs_profile in current_settings.moduleswitches) then
  2628. result:=16
  2629. else
  2630. result:=current_settings.alignment.procalign;
  2631. end;
  2632. procedure gen_pic_helpers(list : TAsmList);
  2633. {$ifdef i386}
  2634. var
  2635. href : treference;
  2636. {$endif i386}
  2637. begin
  2638. { if other cpus require such helpers as well, it can be solved more cleanly }
  2639. {$ifdef i386}
  2640. if current_module.requires_ebx_pic_helper then
  2641. begin
  2642. new_section(list,sec_code,'fpc_geteipasebx',0);
  2643. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2644. reference_reset(href,sizeof(pint));
  2645. href.base:=NR_ESP;
  2646. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2647. list.concat(taicpu.op_none(A_RET,S_NO));
  2648. end;
  2649. if current_module.requires_ecx_pic_helper then
  2650. begin
  2651. new_section(list,sec_code,'fpc_geteipasecx',0);
  2652. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2653. reference_reset(href,sizeof(pint));
  2654. href.base:=NR_ESP;
  2655. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2656. list.concat(taicpu.op_none(A_RET,S_NO));
  2657. end;
  2658. {$endif i386}
  2659. end;
  2660. end.