ncgutil.pas 111 KB

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