ncgutil.pas 110 KB

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