ncgutil.pas 113 KB

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