ncgutil.pas 112 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 }
  1060. if tparavarsym(p).vardef.typ <> variantdef then begin
  1061. location_get_data_ref(list,tparavarsym(p).initialloc,href,is_open_array(tparavarsym(p).vardef));
  1062. cg.g_incrrefcount(list,tparavarsym(p).vardef,href);
  1063. end;
  1064. end;
  1065. vs_out :
  1066. begin
  1067. if needs_inittable or
  1068. do_trashing then
  1069. begin
  1070. tmpreg:=cg.getaddressregister(list);
  1071. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1072. reference_reset_base(href,tmpreg,0);
  1073. if do_trashing and
  1074. { needs separate implementation to trash open arrays }
  1075. { since their size is only known at run time }
  1076. not is_special_array(tparavarsym(p).vardef) then
  1077. trash_reference(list,href,tparavarsym(p).vardef.size);
  1078. if needs_inittable then
  1079. cg.g_initialize(list,tparavarsym(p).vardef,href);
  1080. end;
  1081. end;
  1082. else if do_trashing and
  1083. ([vo_is_funcret,vo_is_hidden_para] * tparavarsym(p).varoptions = [vo_is_funcret,vo_is_hidden_para]) then
  1084. begin
  1085. tmpreg:=cg.getaddressregister(list);
  1086. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).initialloc,tmpreg);
  1087. reference_reset_base(href,tmpreg,0);
  1088. { may be an open string, even if is_open_string() returns }
  1089. { false for some helpers in the system unit }
  1090. if not is_shortstring(tparavarsym(p).vardef) then
  1091. trash_reference(list,href,tparavarsym(p).vardef.size)
  1092. else
  1093. { an open string has at least size 2 }
  1094. trash_reference(list,href,2);
  1095. end
  1096. end;
  1097. end;
  1098. end;
  1099. { generates the code for decrementing the reference count of parameters }
  1100. procedure final_paras(p:TObject;arg:pointer);
  1101. var
  1102. list : TAsmList;
  1103. href : treference;
  1104. begin
  1105. if not(tsym(p).typ=paravarsym) then
  1106. exit;
  1107. list:=TAsmList(arg);
  1108. if not is_class(tparavarsym(p).vardef) and
  1109. tparavarsym(p).vardef.needs_inittable then
  1110. begin
  1111. if (tparavarsym(p).varspez=vs_value) then
  1112. begin
  1113. include(current_procinfo.flags,pi_needs_implicit_finally);
  1114. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vardef));
  1115. cg.g_decrrefcount(list,tparavarsym(p).vardef,href);
  1116. end;
  1117. end;
  1118. { open arrays can contain elements requiring init/final code, so the else has been removed here }
  1119. if (tparavarsym(p).varspez=vs_value) and
  1120. (is_open_array(tparavarsym(p).vardef) or
  1121. is_array_of_const(tparavarsym(p).vardef)) then
  1122. begin
  1123. { cdecl functions don't have a high pointer so it is not possible to generate
  1124. a local copy }
  1125. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1126. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  1127. end;
  1128. end;
  1129. { Initialize temp ansi/widestrings,interfaces }
  1130. procedure inittempvariables(list:TAsmList);
  1131. var
  1132. hp : ptemprecord;
  1133. href : treference;
  1134. begin
  1135. hp:=tg.templist;
  1136. while assigned(hp) do
  1137. begin
  1138. if assigned(hp^.def) and
  1139. hp^.def.needs_inittable then
  1140. begin
  1141. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1142. cg.g_initialize(list,hp^.def,href);
  1143. end;
  1144. hp:=hp^.next;
  1145. end;
  1146. end;
  1147. procedure finalizetempvariables(list:TAsmList);
  1148. var
  1149. hp : ptemprecord;
  1150. href : treference;
  1151. begin
  1152. hp:=tg.templist;
  1153. while assigned(hp) do
  1154. begin
  1155. if assigned(hp^.def) and
  1156. hp^.def.needs_inittable then
  1157. begin
  1158. include(current_procinfo.flags,pi_needs_implicit_finally);
  1159. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  1160. cg.g_finalize(list,hp^.def,href);
  1161. end;
  1162. hp:=hp^.next;
  1163. end;
  1164. end;
  1165. procedure gen_load_return_value(list:TAsmList);
  1166. var
  1167. href : treference;
  1168. ressym : tabstractnormalvarsym;
  1169. resloc,
  1170. restmploc : tlocation;
  1171. hreg : tregister;
  1172. funcretloc : tlocation;
  1173. begin
  1174. { Is the loading needed? }
  1175. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  1176. (
  1177. (po_assembler in current_procinfo.procdef.procoptions) and
  1178. (not(assigned(current_procinfo.procdef.funcretsym)) or
  1179. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  1180. ) then
  1181. exit;
  1182. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1183. { constructors return self }
  1184. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1185. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.Find('self'))
  1186. else
  1187. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1188. if (ressym.refs>0) or (ressym.vardef.needs_inittable) then
  1189. begin
  1190. {$ifdef OLDREGVARS}
  1191. case ressym.localloc.loc of
  1192. LOC_CFPUREGISTER,
  1193. LOC_FPUREGISTER:
  1194. begin
  1195. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1196. restmploc.register:=ressym.localloc.register;
  1197. end;
  1198. LOC_CREGISTER,
  1199. LOC_REGISTER:
  1200. begin
  1201. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1202. restmploc.register:=ressym.localloc.register;
  1203. end;
  1204. LOC_MMREGISTER:
  1205. begin
  1206. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1207. restmploc.register:=ressym.localloc.register;
  1208. end;
  1209. LOC_REFERENCE:
  1210. begin
  1211. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1212. restmploc.reference:=ressym.localloc.reference;
  1213. end;
  1214. else
  1215. internalerror(200309184);
  1216. end;
  1217. {$else}
  1218. restmploc:=ressym.localloc;
  1219. {$endif}
  1220. { Here, we return the function result. In most architectures, the value is
  1221. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1222. function returns in a register and the caller receives it in an other one }
  1223. case funcretloc.loc of
  1224. LOC_REGISTER:
  1225. begin
  1226. {$ifdef cpu64bitaddr}
  1227. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_128,OS_S128] then
  1228. begin
  1229. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1230. if resloc.loc<>LOC_REGISTER then
  1231. internalerror(200409141);
  1232. { Load low and high register separate to generate better register
  1233. allocation info }
  1234. if getsupreg(resloc.register)<first_int_imreg then
  1235. begin
  1236. cg.getcpuregister(list,resloc.register);
  1237. end;
  1238. case restmploc.loc of
  1239. LOC_REFERENCE :
  1240. begin
  1241. href:=restmploc.reference;
  1242. if target_info.endian=ENDIAN_BIG then
  1243. inc(href.offset,8);
  1244. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1245. end;
  1246. LOC_CREGISTER :
  1247. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1248. else
  1249. internalerror(200409203);
  1250. end;
  1251. if getsupreg(resloc.registerhi)<first_int_imreg then
  1252. begin
  1253. cg.getcpuregister(list,resloc.registerhi);
  1254. end;
  1255. case restmploc.loc of
  1256. LOC_REFERENCE :
  1257. begin
  1258. href:=restmploc.reference;
  1259. if target_info.endian=ENDIAN_LITTLE then
  1260. inc(href.offset,8);
  1261. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.registerhi);
  1262. end;
  1263. LOC_CREGISTER :
  1264. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.registerhi,resloc.registerhi);
  1265. else
  1266. internalerror(200409204);
  1267. end;
  1268. end
  1269. else
  1270. { this code is for structures etc. being returned in registers and having odd sizes }
  1271. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_64) and
  1272. (restmploc.size<>OS_64) then
  1273. begin
  1274. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1275. if resloc.loc<>LOC_REGISTER then
  1276. internalerror(200409141);
  1277. { Load low and high register separate to generate better register
  1278. allocation info }
  1279. if getsupreg(resloc.register)<first_int_imreg then
  1280. begin
  1281. cg.getcpuregister(list,resloc.register);
  1282. end;
  1283. case restmploc.loc of
  1284. LOC_REFERENCE :
  1285. begin
  1286. href:=restmploc.reference;
  1287. cg.a_load_ref_reg(list,OS_64,OS_64,href,resloc.register);
  1288. end;
  1289. LOC_CREGISTER :
  1290. cg.a_load_reg_reg(list,OS_64,OS_64,restmploc.register,resloc.register);
  1291. else
  1292. internalerror(200409203);
  1293. end;
  1294. end
  1295. else
  1296. {$else cpu64bitaddr}
  1297. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1298. begin
  1299. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1300. if resloc.loc<>LOC_REGISTER then
  1301. internalerror(200409141);
  1302. { Load low and high register separate to generate better register
  1303. allocation info }
  1304. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1305. begin
  1306. cg.getcpuregister(list,resloc.register64.reglo);
  1307. end;
  1308. case restmploc.loc of
  1309. LOC_REFERENCE :
  1310. begin
  1311. href:=restmploc.reference;
  1312. if target_info.endian=ENDIAN_BIG then
  1313. inc(href.offset,4);
  1314. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1315. end;
  1316. LOC_CREGISTER :
  1317. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1318. else
  1319. internalerror(200409203);
  1320. end;
  1321. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1322. begin
  1323. cg.getcpuregister(list,resloc.register64.reghi);
  1324. end;
  1325. case restmploc.loc of
  1326. LOC_REFERENCE :
  1327. begin
  1328. href:=restmploc.reference;
  1329. if target_info.endian=ENDIAN_LITTLE then
  1330. inc(href.offset,4);
  1331. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1332. end;
  1333. LOC_CREGISTER :
  1334. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1335. else
  1336. internalerror(200409204);
  1337. end;
  1338. end
  1339. else
  1340. {$endif not cpu64bitaddr}
  1341. { this code is for structures etc. being returned in registers and having odd sizes }
  1342. if (current_procinfo.procdef.funcretloc[calleeside].size=OS_32) and
  1343. not(restmploc.size in [OS_S32,OS_32]) then
  1344. begin
  1345. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1346. if resloc.loc<>LOC_REGISTER then
  1347. internalerror(200409141);
  1348. { Load low and high register separate to generate better register
  1349. allocation info }
  1350. if getsupreg(resloc.register)<first_int_imreg then
  1351. begin
  1352. cg.getcpuregister(list,resloc.register);
  1353. end;
  1354. case restmploc.loc of
  1355. LOC_REFERENCE :
  1356. begin
  1357. href:=restmploc.reference;
  1358. resloc.register:=cg.makeregsize(list,resloc.register,OS_32);
  1359. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register);
  1360. end;
  1361. LOC_CREGISTER :
  1362. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register,resloc.register);
  1363. else
  1364. internalerror(200409203);
  1365. end;
  1366. end
  1367. else
  1368. begin
  1369. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1370. if getsupreg(funcretloc.register)<first_int_imreg then
  1371. begin
  1372. cg.getcpuregister(list,funcretloc.register);
  1373. end;
  1374. { it could be that a structure is passed in memory but the function is expected to
  1375. return a pointer to this memory }
  1376. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1377. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1378. else
  1379. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1380. end;
  1381. end;
  1382. LOC_FPUREGISTER:
  1383. begin
  1384. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1385. begin
  1386. cg.getcpuregister(list,funcretloc.register);
  1387. end;
  1388. { we can't do direct moves between fpu and mm registers }
  1389. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1390. location_force_fpureg(list,restmploc,false);
  1391. cg.a_loadfpu_loc_reg(list,funcretloc.size,restmploc,funcretloc.register);
  1392. end;
  1393. LOC_MMREGISTER:
  1394. begin
  1395. if getsupreg(funcretloc.register)<first_mm_imreg then
  1396. begin
  1397. cg.getcpuregister(list,funcretloc.register);
  1398. end;
  1399. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1400. end;
  1401. LOC_INVALID,
  1402. LOC_REFERENCE:
  1403. ;
  1404. else
  1405. internalerror(200405025);
  1406. end;
  1407. end
  1408. {$ifdef x86}
  1409. else
  1410. begin
  1411. { the caller will pop a value off the cpu stack }
  1412. if (funcretloc.loc = LOC_FPUREGISTER) then
  1413. list.concat(taicpu.op_none(A_FLDZ));
  1414. end;
  1415. {$endif x86}
  1416. end;
  1417. procedure gen_alloc_regvar(list:TAsmList;sym: tabstractnormalvarsym);
  1418. begin
  1419. case sym.initialloc.loc of
  1420. LOC_CREGISTER:
  1421. begin
  1422. {$ifndef cpu64bitalu}
  1423. if sym.initialloc.size in [OS_64,OS_S64] then
  1424. begin
  1425. sym.initialloc.register64.reglo:=cg.getintregister(list,OS_32);
  1426. sym.initialloc.register64.reghi:=cg.getintregister(list,OS_32);
  1427. end
  1428. else
  1429. {$endif cpu64bitalu}
  1430. sym.initialloc.register:=cg.getintregister(list,sym.initialloc.size);
  1431. end;
  1432. LOC_CFPUREGISTER:
  1433. begin
  1434. sym.initialloc.register:=cg.getfpuregister(list,sym.initialloc.size);
  1435. end;
  1436. LOC_CMMREGISTER:
  1437. begin
  1438. sym.initialloc.register:=cg.getmmregister(list,sym.initialloc.size);
  1439. end;
  1440. end;
  1441. if (pi_has_goto in current_procinfo.flags) then
  1442. begin
  1443. { Allocate register already, to prevent first allocation to be
  1444. inside a loop }
  1445. {$ifndef cpu64bitalu}
  1446. if sym.initialloc.size in [OS_64,OS_S64] then
  1447. begin
  1448. cg.a_reg_sync(list,sym.initialloc.register64.reglo);
  1449. cg.a_reg_sync(list,sym.initialloc.register64.reghi);
  1450. end
  1451. else
  1452. {$endif not cpu64bitalu}
  1453. cg.a_reg_sync(list,sym.initialloc.register);
  1454. end;
  1455. sym.localloc:=sym.initialloc;
  1456. end;
  1457. procedure gen_load_para_value(list:TAsmList);
  1458. procedure get_para(const paraloc:TCGParaLocation);
  1459. begin
  1460. case paraloc.loc of
  1461. LOC_REGISTER :
  1462. begin
  1463. if getsupreg(paraloc.register)<first_int_imreg then
  1464. cg.getcpuregister(list,paraloc.register);
  1465. end;
  1466. LOC_MMREGISTER :
  1467. begin
  1468. if getsupreg(paraloc.register)<first_mm_imreg then
  1469. cg.getcpuregister(list,paraloc.register);
  1470. end;
  1471. LOC_FPUREGISTER :
  1472. begin
  1473. if getsupreg(paraloc.register)<first_fpu_imreg then
  1474. cg.getcpuregister(list,paraloc.register);
  1475. end;
  1476. end;
  1477. end;
  1478. procedure unget_para(const paraloc:TCGParaLocation);
  1479. begin
  1480. case paraloc.loc of
  1481. LOC_REGISTER :
  1482. begin
  1483. if getsupreg(paraloc.register)<first_int_imreg then
  1484. cg.ungetcpuregister(list,paraloc.register);
  1485. end;
  1486. LOC_MMREGISTER :
  1487. begin
  1488. if getsupreg(paraloc.register)<first_mm_imreg then
  1489. cg.ungetcpuregister(list,paraloc.register);
  1490. end;
  1491. LOC_FPUREGISTER :
  1492. begin
  1493. if getsupreg(paraloc.register)<first_fpu_imreg then
  1494. cg.ungetcpuregister(list,paraloc.register);
  1495. end;
  1496. end;
  1497. end;
  1498. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1499. var
  1500. href : treference;
  1501. begin
  1502. case paraloc.loc of
  1503. LOC_REGISTER :
  1504. begin
  1505. {$IFDEF POWERPC64}
  1506. if (paraloc.shiftval <> 0) then
  1507. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1508. {$ENDIF POWERPC64}
  1509. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1510. end;
  1511. LOC_MMREGISTER :
  1512. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1513. LOC_FPUREGISTER :
  1514. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1515. LOC_REFERENCE :
  1516. begin
  1517. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1518. { use concatcopy, because it can also be a float which fails when
  1519. load_ref_ref is used. Don't copy data when the references are equal }
  1520. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1521. cg.g_concatcopy(list,href,ref,sizeleft);
  1522. end;
  1523. else
  1524. internalerror(2002081302);
  1525. end;
  1526. end;
  1527. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1528. var
  1529. href : treference;
  1530. begin
  1531. case paraloc.loc of
  1532. LOC_REGISTER :
  1533. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1534. LOC_MMREGISTER :
  1535. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1536. LOC_FPUREGISTER :
  1537. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1538. LOC_REFERENCE :
  1539. begin
  1540. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1541. case getregtype(reg) of
  1542. R_INTREGISTER :
  1543. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1544. R_FPUREGISTER :
  1545. cg.a_loadfpu_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1546. R_MMREGISTER :
  1547. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1548. else
  1549. internalerror(2004101012);
  1550. end;
  1551. end;
  1552. else
  1553. internalerror(2002081302);
  1554. end;
  1555. end;
  1556. var
  1557. i : longint;
  1558. currpara : tparavarsym;
  1559. paraloc : pcgparalocation;
  1560. href : treference;
  1561. sizeleft : aint;
  1562. {$if defined(sparc) or defined(arm)}
  1563. tempref : treference;
  1564. {$endif sparc}
  1565. begin
  1566. if (po_assembler in current_procinfo.procdef.procoptions) then
  1567. exit;
  1568. { Allocate registers used by parameters }
  1569. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1570. begin
  1571. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1572. paraloc:=currpara.paraloc[calleeside].location;
  1573. while assigned(paraloc) do
  1574. begin
  1575. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1576. get_para(paraloc^);
  1577. paraloc:=paraloc^.next;
  1578. end;
  1579. end;
  1580. { Copy parameters to local references/registers }
  1581. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1582. begin
  1583. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1584. paraloc:=currpara.paraloc[calleeside].location;
  1585. { skip e.g. empty records }
  1586. if not assigned(paraloc) then
  1587. internalerror(200408203);
  1588. if (paraloc^.loc = LOC_VOID) then
  1589. continue;
  1590. case currpara.initialloc.loc of
  1591. LOC_REFERENCE :
  1592. begin
  1593. { If the parameter location is reused we don't need to copy
  1594. anything }
  1595. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1596. begin
  1597. href:=currpara.initialloc.reference;
  1598. sizeleft:=currpara.paraloc[calleeside].intsize;
  1599. while assigned(paraloc) do
  1600. begin
  1601. if (paraloc^.size=OS_NO) then
  1602. begin
  1603. { Can only be a reference that contains the rest
  1604. of the parameter }
  1605. if (paraloc^.loc<>LOC_REFERENCE) or
  1606. assigned(paraloc^.next) then
  1607. internalerror(2005013010);
  1608. gen_load_ref(paraloc^,href,sizeleft);
  1609. inc(href.offset,sizeleft);
  1610. sizeleft:=0;
  1611. end
  1612. else
  1613. begin
  1614. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1615. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1616. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1617. end;
  1618. unget_para(paraloc^);
  1619. paraloc:=paraloc^.next;
  1620. end;
  1621. end;
  1622. end;
  1623. LOC_CREGISTER :
  1624. begin
  1625. {$ifndef cpu64bitalu}
  1626. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1627. is_64bit(currpara.vardef) then
  1628. begin
  1629. case paraloc^.loc of
  1630. LOC_REGISTER:
  1631. begin
  1632. if not assigned(paraloc^.next) then
  1633. internalerror(200410104);
  1634. if (target_info.endian=ENDIAN_BIG) then
  1635. begin
  1636. { paraloc^ -> high
  1637. paraloc^.next -> low }
  1638. unget_para(paraloc^);
  1639. gen_alloc_regvar(list,currpara);
  1640. gen_load_reg(paraloc^,currpara.initialloc.register64.reghi);
  1641. unget_para(paraloc^.next^);
  1642. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reglo);
  1643. end
  1644. else
  1645. begin
  1646. { paraloc^ -> low
  1647. paraloc^.next -> high }
  1648. unget_para(paraloc^);
  1649. gen_alloc_regvar(list,currpara);
  1650. gen_load_reg(paraloc^,currpara.initialloc.register64.reglo);
  1651. unget_para(paraloc^.next^);
  1652. gen_load_reg(paraloc^.next^,currpara.initialloc.register64.reghi);
  1653. end;
  1654. end;
  1655. LOC_REFERENCE:
  1656. begin
  1657. gen_alloc_regvar(list,currpara);
  1658. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1659. cg64.a_load64_ref_reg(list,href,currpara.initialloc.register64);
  1660. unget_para(paraloc^);
  1661. end;
  1662. else
  1663. internalerror(2005101501);
  1664. end
  1665. end
  1666. else
  1667. {$endif not cpu64bitalu}
  1668. begin
  1669. if assigned(paraloc^.next) then
  1670. internalerror(200410105);
  1671. unget_para(paraloc^);
  1672. gen_alloc_regvar(list,currpara);
  1673. gen_load_reg(paraloc^,currpara.initialloc.register);
  1674. end;
  1675. end;
  1676. LOC_CFPUREGISTER :
  1677. begin
  1678. {$if defined(sparc) or defined(arm)}
  1679. { Arm and Sparc passes floats in int registers, when loading to fpu register
  1680. we need a temp }
  1681. sizeleft := TCGSize2Size[currpara.initialloc.size];
  1682. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1683. href:=tempref;
  1684. while assigned(paraloc) do
  1685. begin
  1686. unget_para(paraloc^);
  1687. gen_load_ref(paraloc^,href,sizeleft);
  1688. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1689. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1690. paraloc:=paraloc^.next;
  1691. end;
  1692. gen_alloc_regvar(list,currpara);
  1693. cg.a_loadfpu_ref_reg(list,currpara.initialloc.size,currpara.initialloc.size,tempref,currpara.initialloc.register);
  1694. tg.UnGetTemp(list,tempref);
  1695. {$else sparc}
  1696. unget_para(paraloc^);
  1697. gen_alloc_regvar(list,currpara);
  1698. gen_load_reg(paraloc^,currpara.initialloc.register);
  1699. if assigned(paraloc^.next) then
  1700. internalerror(200410109);
  1701. {$endif sparc}
  1702. end;
  1703. LOC_CMMREGISTER :
  1704. begin
  1705. unget_para(paraloc^);
  1706. gen_alloc_regvar(list,currpara);
  1707. gen_load_reg(paraloc^,currpara.initialloc.register);
  1708. { data could come in two memory locations, for now
  1709. we simply ignore the sanity check (FK)
  1710. if assigned(paraloc^.next) then
  1711. internalerror(200410108);
  1712. }
  1713. end;
  1714. end;
  1715. end;
  1716. { generate copies of call by value parameters, must be done before
  1717. the initialization and body is parsed because the refcounts are
  1718. incremented using the local copies }
  1719. current_procinfo.procdef.parast.SymList.ForEachCall(@copyvalueparas,list);
  1720. {$ifdef powerpc}
  1721. { unget the register that contains the stack pointer before the procedure entry, }
  1722. { which is used to access the parameters in their original callee-side location }
  1723. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1724. cg.a_reg_dealloc(list,NR_R12);
  1725. {$endif powerpc}
  1726. {$ifdef powerpc64}
  1727. { unget the register that contains the stack pointer before the procedure entry, }
  1728. { which is used to access the parameters in their original callee-side location }
  1729. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1730. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1731. {$endif powerpc64}
  1732. end;
  1733. procedure gen_initialize_code(list:TAsmList);
  1734. begin
  1735. { initialize local data like ansistrings }
  1736. case current_procinfo.procdef.proctypeoption of
  1737. potype_unitinit:
  1738. begin
  1739. { this is also used for initialization of variables in a
  1740. program which does not have a globalsymtable }
  1741. if assigned(current_module.globalsymtable) then
  1742. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@initialize_data,list);
  1743. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_data,list);
  1744. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1745. end;
  1746. { units have seperate code for initilization and finalization }
  1747. potype_unitfinalize: ;
  1748. { program init/final is generated in separate procedure }
  1749. potype_proginit:
  1750. begin
  1751. TSymtable(current_module.localsymtable).SymList.ForEachCall(@initialize_regvars,list);
  1752. end;
  1753. else
  1754. begin
  1755. if (localvartrashing <> -1) and
  1756. not(po_assembler in current_procinfo.procdef.procoptions) then
  1757. current_procinfo.procdef.localst.SymList.ForEachCall(@trash_variable,list);
  1758. current_procinfo.procdef.localst.SymList.ForEachCall(@initialize_data,list);
  1759. end;
  1760. end;
  1761. { initialisizes temp. ansi/wide string data }
  1762. inittempvariables(list);
  1763. { initialize ansi/widesstring para's }
  1764. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1765. current_procinfo.procdef.parast.SymList.ForEachCall(@init_paras,list);
  1766. {$ifdef OLDREGVARS}
  1767. load_regvars(list,nil);
  1768. {$endif OLDREGVARS}
  1769. end;
  1770. procedure gen_finalize_code(list:TAsmList);
  1771. begin
  1772. {$ifdef OLDREGVARS}
  1773. cleanup_regvars(list);
  1774. {$endif OLDREGVARS}
  1775. { finalize temporary data }
  1776. finalizetempvariables(list);
  1777. { finalize local data like ansistrings}
  1778. case current_procinfo.procdef.proctypeoption of
  1779. potype_unitfinalize:
  1780. begin
  1781. { this is also used for initialization of variables in a
  1782. program which does not have a globalsymtable }
  1783. if assigned(current_module.globalsymtable) then
  1784. TSymtable(current_module.globalsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1785. TSymtable(current_module.localsymtable).SymList.ForEachCall(@finalize_static_data,list);
  1786. end;
  1787. { units/progs have separate code for initialization and finalization }
  1788. potype_unitinit: ;
  1789. { program init/final is generated in separate procedure }
  1790. potype_proginit: ;
  1791. else
  1792. current_procinfo.procdef.localst.SymList.ForEachCall(@finalize_local_vars,list);
  1793. end;
  1794. { finalize paras data }
  1795. if assigned(current_procinfo.procdef.parast) and
  1796. not(po_assembler in current_procinfo.procdef.procoptions) then
  1797. current_procinfo.procdef.parast.SymList.ForEachCall(@final_paras,list);
  1798. end;
  1799. procedure gen_entry_code(list:TAsmList);
  1800. var
  1801. paraloc1,
  1802. paraloc2 : tcgpara;
  1803. begin
  1804. paraloc1.init;
  1805. paraloc2.init;
  1806. { the actual profile code can clobber some registers,
  1807. therefore if the context must be saved, do it before
  1808. the actual call to the profile code
  1809. }
  1810. if (cs_profile in current_settings.moduleswitches) and
  1811. not(po_assembler in current_procinfo.procdef.procoptions) then
  1812. begin
  1813. { non-win32 can call mcout even in main }
  1814. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1815. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1816. begin
  1817. cg.g_profilecode(list);
  1818. end;
  1819. end;
  1820. { call startup helpers from main program }
  1821. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1822. begin
  1823. { initialize units }
  1824. cg.allocallcpuregisters(list);
  1825. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1826. cg.deallocallcpuregisters(list);
  1827. end;
  1828. list.concat(Tai_force_line.Create);
  1829. {$ifdef OLDREGVARS}
  1830. load_regvars(list,nil);
  1831. {$endif OLDREGVARS}
  1832. paraloc1.done;
  1833. paraloc2.done;
  1834. end;
  1835. procedure gen_exit_code(list:TAsmList);
  1836. begin
  1837. { call __EXIT for main program }
  1838. if (not DLLsource) and
  1839. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1840. cg.a_call_name(list,'FPC_DO_EXIT');
  1841. end;
  1842. {****************************************************************************
  1843. Entry/Exit
  1844. ****************************************************************************}
  1845. function has_alias_name(pd:tprocdef;const s:string):boolean;
  1846. var
  1847. item : TCmdStrListItem;
  1848. begin
  1849. result:=true;
  1850. if pd.mangledname=s then
  1851. exit;
  1852. item := TCmdStrListItem(pd.aliasnames.first);
  1853. while assigned(item) do
  1854. begin
  1855. if item.str=s then
  1856. exit;
  1857. item := TCmdStrListItem(item.next);
  1858. end;
  1859. result:=false;
  1860. end;
  1861. procedure alloc_proc_symbol(pd: tprocdef);
  1862. var
  1863. item : TCmdStrListItem;
  1864. begin
  1865. item := TCmdStrListItem(pd.aliasnames.first);
  1866. while assigned(item) do
  1867. begin
  1868. current_asmdata.DefineAsmSymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1869. item := TCmdStrListItem(item.next);
  1870. end;
  1871. end;
  1872. procedure gen_proc_symbol(list:TAsmList);
  1873. var
  1874. item,
  1875. previtem : TCmdStrListItem;
  1876. begin
  1877. previtem:=nil;
  1878. item := TCmdStrListItem(current_procinfo.procdef.aliasnames.first);
  1879. while assigned(item) do
  1880. begin
  1881. { "double link" all procedure entry symbols via .reference }
  1882. { directives on darwin, because otherwise the linker }
  1883. { sometimes strips the procedure if only on of the symbols }
  1884. { is referenced }
  1885. if assigned(previtem) and
  1886. (target_info.system in systems_darwin) then
  1887. list.concat(tai_directive.create(asd_reference,item.str));
  1888. if (cs_profile in current_settings.moduleswitches) or
  1889. (po_global in current_procinfo.procdef.procoptions) then
  1890. list.concat(Tai_symbol.createname_global(item.str,AT_FUNCTION,0))
  1891. else
  1892. list.concat(Tai_symbol.createname(item.str,AT_FUNCTION,0));
  1893. if assigned(previtem) and
  1894. (target_info.system in systems_darwin) then
  1895. list.concat(tai_directive.create(asd_reference,previtem.str));
  1896. if tf_use_function_relative_addresses in target_info.flags then
  1897. list.concat(Tai_function_name.create(item.str));
  1898. previtem:=item;
  1899. item := TCmdStrListItem(item.next);
  1900. end;
  1901. current_procinfo.procdef.procstarttai:=tai(list.last);
  1902. end;
  1903. procedure gen_proc_symbol_end(list:TAsmList);
  1904. begin
  1905. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1906. current_procinfo.procdef.procendtai:=tai(list.last);
  1907. if (current_module.islibrary) then
  1908. if (current_procinfo.procdef.proctypeoption = potype_proginit) then
  1909. exportlib.setinitname(list,current_procinfo.procdef.mangledname)
  1910. else if ((current_module.flags and uf_finalize)<>0) then
  1911. exportlib.setfininame(list,current_procinfo.procdef.mangledname);
  1912. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1913. begin
  1914. if (target_info.system in (systems_darwin+[system_powerpc_macos])) and
  1915. not(current_module.islibrary) then
  1916. begin
  1917. list.concat(tai_section.create(sec_code,'',4));
  1918. list.concat(tai_symbol.createname_global(
  1919. target_info.cprefix+mainaliasname,AT_FUNCTION,0));
  1920. { keep argc, argv and envp properly on the stack }
  1921. cg.a_jmp_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1922. end;
  1923. end;
  1924. end;
  1925. procedure gen_proc_entry_code(list:TAsmList);
  1926. var
  1927. hitemp,
  1928. lotemp : longint;
  1929. begin
  1930. { generate call frame marker for dwarf call frame info }
  1931. current_asmdata.asmcfi.start_frame(list);
  1932. { All temps are know, write offsets used for information }
  1933. if (cs_asm_source in current_settings.globalswitches) then
  1934. begin
  1935. if tg.direction>0 then
  1936. begin
  1937. lotemp:=current_procinfo.tempstart;
  1938. hitemp:=tg.lasttemp;
  1939. end
  1940. else
  1941. begin
  1942. lotemp:=tg.lasttemp;
  1943. hitemp:=current_procinfo.tempstart;
  1944. end;
  1945. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1946. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1947. end;
  1948. { generate target specific proc entry code }
  1949. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1950. end;
  1951. procedure gen_proc_exit_code(list:TAsmList);
  1952. var
  1953. parasize : longint;
  1954. begin
  1955. { c style clearstack does not need to remove parameters from the stack, only the
  1956. return value when it was pushed by arguments }
  1957. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1958. begin
  1959. parasize:=0;
  1960. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1961. inc(parasize,sizeof(pint));
  1962. end
  1963. else
  1964. parasize:=current_procinfo.para_stack_size;
  1965. { generate target specific proc exit code }
  1966. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1967. { release return registers, needed for optimizer }
  1968. if not is_void(current_procinfo.procdef.returndef) then
  1969. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1970. { end of frame marker for call frame info }
  1971. current_asmdata.asmcfi.end_frame(list);
  1972. end;
  1973. procedure gen_stack_check_size_para(list:TAsmList);
  1974. var
  1975. paraloc1 : tcgpara;
  1976. begin
  1977. paraloc1.init;
  1978. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1979. paramanager.allocparaloc(list,paraloc1);
  1980. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1981. paramanager.freeparaloc(list,paraloc1);
  1982. paraloc1.done;
  1983. end;
  1984. procedure gen_stack_check_call(list:TAsmList);
  1985. var
  1986. paraloc1 : tcgpara;
  1987. begin
  1988. paraloc1.init;
  1989. { Also alloc the register needed for the parameter }
  1990. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1991. paramanager.allocparaloc(list,paraloc1);
  1992. paramanager.freeparaloc(list,paraloc1);
  1993. { Call the helper }
  1994. cg.allocallcpuregisters(list);
  1995. cg.a_call_name(list,'FPC_STACKCHECK');
  1996. cg.deallocallcpuregisters(list);
  1997. paraloc1.done;
  1998. end;
  1999. procedure gen_save_used_regs(list:TAsmList);
  2000. begin
  2001. { Pure assembler routines need to save the registers themselves }
  2002. if (po_assembler in current_procinfo.procdef.procoptions) then
  2003. exit;
  2004. { oldfpccall expects all registers to be destroyed }
  2005. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2006. cg.g_save_registers(list);
  2007. end;
  2008. procedure gen_restore_used_regs(list:TAsmList);
  2009. begin
  2010. { Pure assembler routines need to save the registers themselves }
  2011. if (po_assembler in current_procinfo.procdef.procoptions) then
  2012. exit;
  2013. { oldfpccall expects all registers to be destroyed }
  2014. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  2015. cg.g_restore_registers(list);
  2016. end;
  2017. {****************************************************************************
  2018. External handling
  2019. ****************************************************************************}
  2020. procedure gen_external_stub(list:TAsmList;pd:tprocdef;const externalname:string);
  2021. begin
  2022. { add the procedure to the al_procedures }
  2023. maybe_new_object_file(list);
  2024. new_section(list,sec_code,lower(pd.mangledname),current_settings.alignment.procalign);
  2025. list.concat(Tai_align.create(current_settings.alignment.procalign));
  2026. if (po_global in pd.procoptions) then
  2027. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  2028. else
  2029. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  2030. cg.g_external_wrapper(list,pd,externalname);
  2031. end;
  2032. {****************************************************************************
  2033. Const Data
  2034. ****************************************************************************}
  2035. procedure insertbssdata(sym : tstaticvarsym);
  2036. var
  2037. l : aint;
  2038. varalign : shortint;
  2039. storefilepos : tfileposinfo;
  2040. list : TAsmList;
  2041. sectype : TAsmSectiontype;
  2042. begin
  2043. storefilepos:=current_filepos;
  2044. current_filepos:=sym.fileinfo;
  2045. l:=sym.getsize;
  2046. varalign:=sym.vardef.alignment;
  2047. if (varalign=0) then
  2048. varalign:=l;
  2049. if tf_section_threadvars in target_info.flags then
  2050. begin
  2051. if (vo_is_thread_var in sym.varoptions) then
  2052. begin
  2053. list:=current_asmdata.asmlists[al_threadvars];
  2054. sectype:=sec_threadvar;
  2055. end
  2056. else
  2057. begin
  2058. list:=current_asmdata.asmlists[al_globals];
  2059. sectype:=sec_bss;
  2060. end;
  2061. end
  2062. else
  2063. begin
  2064. if (vo_is_thread_var in sym.varoptions) then
  2065. begin
  2066. inc(l,sizeof(pint));
  2067. { it doesn't help to set a higher alignment, as }
  2068. { the first sizeof(pint) bytes field will offset }
  2069. { everything anyway }
  2070. varalign:=sizeof(pint);
  2071. end;
  2072. list:=current_asmdata.asmlists[al_globals];
  2073. sectype:=sec_bss;
  2074. end;
  2075. varalign:=var_align(varalign);
  2076. maybe_new_object_file(list);
  2077. new_section(list,sectype,lower(sym.mangledname),varalign);
  2078. if (sym.owner.symtabletype=globalsymtable) or
  2079. create_smartlink or
  2080. DLLSource or
  2081. (assigned(current_procinfo) and
  2082. (po_inline in current_procinfo.procdef.procoptions)) or
  2083. (vo_is_public in sym.varoptions) then
  2084. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  2085. else
  2086. list.concat(Tai_datablock.create(sym.mangledname,l));
  2087. current_filepos:=storefilepos;
  2088. end;
  2089. procedure gen_alloc_symtable(list:TAsmList;st:TSymtable);
  2090. procedure setlocalloc(vs:tabstractnormalvarsym);
  2091. begin
  2092. if cs_asm_source in current_settings.globalswitches then
  2093. begin
  2094. case vs.initialloc.loc of
  2095. LOC_REFERENCE :
  2096. begin
  2097. if not assigned(vs.initialloc.reference.symbol) then
  2098. list.concat(Tai_comment.Create(strpnew('Var '+vs.realname+' located at '+
  2099. std_regname(vs.initialloc.reference.base)+tostr_with_plus(vs.initialloc.reference.offset))));
  2100. end;
  2101. end;
  2102. end;
  2103. vs.localloc:=vs.initialloc;
  2104. end;
  2105. var
  2106. i : longint;
  2107. sym : tsym;
  2108. vs : tabstractnormalvarsym;
  2109. isaddr : boolean;
  2110. begin
  2111. for i:=0 to st.SymList.Count-1 do
  2112. begin
  2113. sym:=tsym(st.SymList[i]);
  2114. case sym.typ of
  2115. staticvarsym :
  2116. begin
  2117. vs:=tabstractnormalvarsym(sym);
  2118. { The code in laodnode.pass_generatecode will create the
  2119. LOC_REFERENCE instead for all none register variables. This is
  2120. required because we can't store an asmsymbol in the localloc because
  2121. the asmsymbol is invalid after an unit is compiled. This gives
  2122. problems when this procedure is inlined in an other unit (PFV) }
  2123. if vs.is_regvar(false) then
  2124. begin
  2125. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2126. vs.initialloc.size:=def_cgsize(vs.vardef);
  2127. gen_alloc_regvar(list,vs);
  2128. setlocalloc(vs);
  2129. end;
  2130. end;
  2131. paravarsym :
  2132. begin
  2133. vs:=tabstractnormalvarsym(sym);
  2134. { Parameters passed to assembler procedures need to be kept
  2135. in the original location }
  2136. if (po_assembler in current_procinfo.procdef.procoptions) then
  2137. tparavarsym(vs).paraloc[calleeside].get_location(vs.initialloc)
  2138. else
  2139. begin
  2140. isaddr:=paramanager.push_addr_param(vs.varspez,vs.vardef,current_procinfo.procdef.proccalloption);
  2141. if isaddr then
  2142. vs.initialloc.size:=OS_ADDR
  2143. else
  2144. vs.initialloc.size:=def_cgsize(vs.vardef);
  2145. if vs.is_regvar(isaddr) then
  2146. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable]
  2147. else
  2148. begin
  2149. vs.initialloc.loc:=LOC_REFERENCE;
  2150. { Reuse the parameter location for values to are at a single location on the stack }
  2151. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  2152. begin
  2153. reference_reset_base(vs.initialloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  2154. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  2155. end
  2156. else
  2157. begin
  2158. if isaddr then
  2159. tg.GetLocal(list,sizeof(pint),voidpointertype,vs.initialloc.reference)
  2160. else
  2161. tg.GetLocal(list,vs.getsize,tparavarsym(sym).paraloc[calleeside].alignment,vs.vardef,vs.initialloc.reference);
  2162. end;
  2163. end;
  2164. end;
  2165. setlocalloc(vs);
  2166. end;
  2167. localvarsym :
  2168. begin
  2169. vs:=tabstractnormalvarsym(sym);
  2170. vs.initialloc.size:=def_cgsize(vs.vardef);
  2171. if (m_delphi in current_settings.modeswitches) and
  2172. (po_assembler in current_procinfo.procdef.procoptions) and
  2173. (vo_is_funcret in vs.varoptions) and
  2174. (vs.refs=0) then
  2175. begin
  2176. { not referenced, so don't allocate. Use dummy to }
  2177. { avoid ie's later on because of LOC_INVALID }
  2178. vs.initialloc.loc:=LOC_REGISTER;
  2179. vs.initialloc.size:=OS_INT;
  2180. vs.initialloc.register:=NR_FUNCTION_RESULT_REG;
  2181. end
  2182. else if vs.is_regvar(false) then
  2183. begin
  2184. vs.initialloc.loc:=tvarregable2tcgloc[vs.varregable];
  2185. gen_alloc_regvar(list,vs);
  2186. end
  2187. else
  2188. begin
  2189. vs.initialloc.loc:=LOC_REFERENCE;
  2190. tg.GetLocal(list,vs.getsize,vs.vardef,vs.initialloc.reference);
  2191. end;
  2192. setlocalloc(vs);
  2193. end;
  2194. end;
  2195. end;
  2196. end;
  2197. procedure add_regvars(var rv: tusedregvars; const location: tlocation);
  2198. begin
  2199. case location.loc of
  2200. LOC_CREGISTER:
  2201. {$ifndef cpu64bitalu}
  2202. if location.size in [OS_64,OS_S64] then
  2203. begin
  2204. rv.intregvars.addnodup(getsupreg(location.register64.reglo));
  2205. rv.intregvars.addnodup(getsupreg(location.register64.reghi));
  2206. end
  2207. else
  2208. {$endif not cpu64bitalu}
  2209. rv.intregvars.addnodup(getsupreg(location.register));
  2210. LOC_CFPUREGISTER:
  2211. rv.fpuregvars.addnodup(getsupreg(location.register));
  2212. LOC_CMMREGISTER:
  2213. rv.mmregvars.addnodup(getsupreg(location.register));
  2214. end;
  2215. end;
  2216. function do_get_used_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  2217. var
  2218. rv: pusedregvars absolute arg;
  2219. begin
  2220. case (n.nodetype) of
  2221. temprefn:
  2222. { We only have to synchronise a tempnode before a loop if it is }
  2223. { not created inside the loop, and only synchronise after the }
  2224. { loop if it's not destroyed inside the loop. If it's created }
  2225. { before the loop and not yet destroyed, then before the loop }
  2226. { is secondpassed tempinfo^.valid will be true, and we get the }
  2227. { correct registers. If it's not destroyed inside the loop, }
  2228. { then after the loop has been secondpassed tempinfo^.valid }
  2229. { be true and we also get the right registers. In other cases, }
  2230. { tempinfo^.valid will be false and so we do not add }
  2231. { unnecessary registers. This way, we don't have to look at }
  2232. { tempcreate and tempdestroy nodes to get this info (JM) }
  2233. if (ti_valid in ttemprefnode(n).tempinfo^.flags) then
  2234. add_regvars(rv^,ttemprefnode(n).tempinfo^.location);
  2235. loadn:
  2236. if (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2237. add_regvars(rv^,tabstractnormalvarsym(tloadnode(n).symtableentry).localloc);
  2238. vecn:
  2239. { range checks sometimes need the high parameter }
  2240. if (cs_check_range in current_settings.localswitches) and
  2241. (is_open_array(tvecnode(n).left.resultdef) or
  2242. is_array_of_const(tvecnode(n).left.resultdef)) and
  2243. not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2244. add_regvars(rv^,tabstractnormalvarsym(get_high_value_sym(tparavarsym(tloadnode(tvecnode(n).left).symtableentry))).localloc)
  2245. end;
  2246. result := fen_true;
  2247. end;
  2248. procedure get_used_regvars(n: tnode; var rv: tusedregvars);
  2249. begin
  2250. foreachnodestatic(n,@do_get_used_regvars,@rv);
  2251. end;
  2252. (*
  2253. See comments at declaration of pusedregvarscommon
  2254. function do_get_used_regvars_common(var n: tnode; arg: pointer): foreachnoderesult;
  2255. var
  2256. rv: pusedregvarscommon absolute arg;
  2257. begin
  2258. if (n.nodetype = loadn) and
  2259. (tloadnode(n).symtableentry.typ in [staticvarsym,localvarsym,paravarsym]) then
  2260. with tabstractnormalvarsym(tloadnode(n).symtableentry).localloc do
  2261. case loc of
  2262. LOC_CREGISTER:
  2263. { if not yet encountered in this node tree }
  2264. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2265. { but nevertheless already encountered somewhere }
  2266. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2267. { then it's a regvar used in two or more node trees }
  2268. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2269. LOC_CFPUREGISTER:
  2270. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2271. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2272. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2273. LOC_CMMREGISTER:
  2274. if (rv^.myregvars.intregvars.addnodup(getsupreg(register))) and
  2275. not(rv^.allregvars.intregvars.addnodup(getsupreg(register))) then
  2276. rv^.commonregvars.intregvars.addnodup(getsupreg(register));
  2277. end;
  2278. result := fen_true;
  2279. end;
  2280. procedure get_used_regvars_common(n: tnode; var rv: tusedregvarscommon);
  2281. begin
  2282. rv.myregvars.intregvars.clear;
  2283. rv.myregvars.fpuregvars.clear;
  2284. rv.myregvars.mmregvars.clear;
  2285. foreachnodestatic(n,@do_get_used_regvars_common,@rv);
  2286. end;
  2287. *)
  2288. procedure gen_sync_regvars(list:TAsmList; var rv: tusedregvars);
  2289. var
  2290. count: longint;
  2291. begin
  2292. for count := 1 to rv.intregvars.length do
  2293. cg.a_reg_sync(list,newreg(R_INTREGISTER,rv.intregvars.readidx(count-1),R_SUBWHOLE));
  2294. for count := 1 to rv.fpuregvars.length do
  2295. cg.a_reg_sync(list,newreg(R_FPUREGISTER,rv.fpuregvars.readidx(count-1),R_SUBWHOLE));
  2296. for count := 1 to rv.mmregvars.length do
  2297. cg.a_reg_sync(list,newreg(R_MMREGISTER,rv.mmregvars.readidx(count-1),R_SUBWHOLE));
  2298. end;
  2299. {*****************************************************************************
  2300. SSA support
  2301. *****************************************************************************}
  2302. type
  2303. preplaceregrec = ^treplaceregrec;
  2304. treplaceregrec = record
  2305. old, new: tregister;
  2306. {$ifndef cpu64bitalu}
  2307. oldhi, newhi: tregister;
  2308. {$endif not cpu64bitalu}
  2309. ressym: tsym;
  2310. end;
  2311. function doreplace(var n: tnode; para: pointer): foreachnoderesult;
  2312. var
  2313. rr: preplaceregrec absolute para;
  2314. begin
  2315. result := fen_false;
  2316. if (nf_is_funcret in n.flags) and (fc_exit in flowcontrol) then
  2317. exit;
  2318. case n.nodetype of
  2319. loadn:
  2320. begin
  2321. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  2322. not assigned(tloadnode(n).left) and
  2323. (tloadnode(n).symtableentry <> rr^.ressym) and
  2324. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2325. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register = rr^.old) then
  2326. begin
  2327. {$ifndef cpu64bitalu}
  2328. { it's possible a 64 bit location was shifted and/xor typecasted }
  2329. { in a 32 bit value, so only 1 register was left in the location }
  2330. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.size in [OS_64,OS_S64]) then
  2331. if (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi = rr^.oldhi) then
  2332. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register64.reghi := rr^.newhi
  2333. else
  2334. exit;
  2335. {$endif not cpu64bitalu}
  2336. tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.register := rr^.new;
  2337. result := fen_norecurse_true;
  2338. end;
  2339. end;
  2340. temprefn:
  2341. begin
  2342. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  2343. (ttemprefnode(n).tempinfo^.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) and
  2344. (ttemprefnode(n).tempinfo^.location.register = rr^.old) then
  2345. begin
  2346. {$ifndef cpu64bitalu}
  2347. { it's possible a 64 bit location was shifted and/xor typecasted }
  2348. { in a 32 bit value, so only 1 register was left in the location }
  2349. if (ttemprefnode(n).tempinfo^.location.size in [OS_64,OS_S64]) then
  2350. if (ttemprefnode(n).tempinfo^.location.register64.reghi = rr^.oldhi) then
  2351. ttemprefnode(n).tempinfo^.location.register64.reghi := rr^.newhi
  2352. else
  2353. exit;
  2354. {$endif not cpu64bitalu}
  2355. ttemprefnode(n).tempinfo^.location.register := rr^.new;
  2356. result := fen_norecurse_true;
  2357. end;
  2358. end;
  2359. { optimize the searching a bit }
  2360. derefn,addrn,
  2361. calln,inlinen,casen,
  2362. addn,subn,muln,
  2363. andn,orn,xorn,
  2364. ltn,lten,gtn,gten,equaln,unequaln,
  2365. slashn,divn,shrn,shln,notn,
  2366. inn,
  2367. asn,isn:
  2368. result := fen_norecurse_false;
  2369. end;
  2370. end;
  2371. procedure maybechangeloadnodereg(list: TAsmList; var n: tnode; reload: boolean);
  2372. var
  2373. rr: treplaceregrec;
  2374. begin
  2375. if not (n.location.loc in [LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMXREGISTER,LOC_CMMREGISTER]) or
  2376. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) then
  2377. exit;
  2378. rr.old := n.location.register;
  2379. rr.ressym := nil;
  2380. {$ifndef cpu64bitalu}
  2381. rr.oldhi := NR_NO;
  2382. {$endif not cpu64bitalu}
  2383. case n.location.loc of
  2384. LOC_CREGISTER:
  2385. begin
  2386. {$ifndef cpu64bitalu}
  2387. if (n.location.size in [OS_64,OS_S64]) then
  2388. begin
  2389. rr.oldhi := n.location.register64.reghi;
  2390. rr.new := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2391. rr.newhi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  2392. end
  2393. else
  2394. {$endif not cpu64bitalu}
  2395. rr.new := cg.getintregister(current_asmdata.CurrAsmList,n.location.size);
  2396. end;
  2397. LOC_CFPUREGISTER:
  2398. rr.new := cg.getfpuregister(current_asmdata.CurrAsmList,n.location.size);
  2399. {$ifdef SUPPORT_MMX}
  2400. LOC_CMMXREGISTER:
  2401. rr.new := tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  2402. {$endif SUPPORT_MMX}
  2403. LOC_CMMREGISTER:
  2404. rr.new := cg.getmmregister(current_asmdata.CurrAsmList,n.location.size);
  2405. else
  2406. exit;
  2407. end;
  2408. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  2409. assigned(current_procinfo.procdef.funcretsym) and
  2410. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  2411. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  2412. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  2413. else
  2414. rr.ressym:=current_procinfo.procdef.funcretsym;
  2415. if not foreachnodestatic(n,@doreplace,@rr) then
  2416. exit;
  2417. if reload then
  2418. case n.location.loc of
  2419. LOC_CREGISTER:
  2420. begin
  2421. {$ifndef cpu64bitalu}
  2422. if (n.location.size in [OS_64,OS_S64]) then
  2423. cg64.a_load64_reg_reg(list,n.location.register64,joinreg64(rr.new,rr.newhi))
  2424. else
  2425. {$endif not cpu64bitalu}
  2426. cg.a_load_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2427. end;
  2428. LOC_CFPUREGISTER:
  2429. cg.a_loadfpu_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new);
  2430. {$ifdef SUPPORT_MMX}
  2431. LOC_CMMXREGISTER:
  2432. cg.a_loadmm_reg_reg(list,OS_M64,OS_M64,n.location.register,rr.new,nil);
  2433. {$endif SUPPORT_MMX}
  2434. LOC_CMMREGISTER:
  2435. cg.a_loadmm_reg_reg(list,n.location.size,n.location.size,n.location.register,rr.new,nil);
  2436. else
  2437. internalerror(2006090920);
  2438. end;
  2439. { now that we've change the loadn/temp, also change the node result location }
  2440. {$ifndef cpu64bitalu}
  2441. if (n.location.size in [OS_64,OS_S64]) then
  2442. begin
  2443. n.location.register64.reglo := rr.new;
  2444. n.location.register64.reghi := rr.newhi;
  2445. end
  2446. else
  2447. {$endif not cpu64bitalu}
  2448. n.location.register := rr.new;
  2449. end;
  2450. procedure gen_free_symtable(list:TAsmList;st:TSymtable);
  2451. var
  2452. i : longint;
  2453. sym : tsym;
  2454. begin
  2455. for i:=0 to st.SymList.Count-1 do
  2456. begin
  2457. sym:=tsym(st.SymList[i]);
  2458. if (sym.typ in [staticvarsym,localvarsym,paravarsym]) then
  2459. begin
  2460. with tabstractnormalvarsym(sym) do
  2461. begin
  2462. { Note: We need to keep the data available in memory
  2463. for the sub procedures that can access local data
  2464. in the parent procedures }
  2465. case localloc.loc of
  2466. LOC_CREGISTER :
  2467. if (pi_has_goto in current_procinfo.flags) then
  2468. {$ifndef cpu64bitalu}
  2469. if def_cgsize(vardef) in [OS_64,OS_S64] then
  2470. begin
  2471. cg.a_reg_sync(list,localloc.register64.reglo);
  2472. cg.a_reg_sync(list,localloc.register64.reghi);
  2473. end
  2474. else
  2475. {$endif not cpu64bitalu}
  2476. cg.a_reg_sync(list,localloc.register);
  2477. LOC_CFPUREGISTER,
  2478. LOC_CMMREGISTER:
  2479. if (pi_has_goto in current_procinfo.flags) then
  2480. cg.a_reg_sync(list,localloc.register);
  2481. LOC_REFERENCE :
  2482. begin
  2483. if typ in [localvarsym,paravarsym] then
  2484. tg.Ungetlocal(list,localloc.reference);
  2485. end;
  2486. end;
  2487. end;
  2488. end;
  2489. end;
  2490. end;
  2491. procedure gen_intf_wrapper(list:TAsmList;_class:tobjectdef);
  2492. var
  2493. i,j : longint;
  2494. tmps : string;
  2495. pd : TProcdef;
  2496. ImplIntf : TImplementedInterface;
  2497. begin
  2498. for i:=0 to _class.ImplementedInterfaces.count-1 do
  2499. begin
  2500. ImplIntf:=TImplementedInterface(_class.ImplementedInterfaces[i]);
  2501. if (ImplIntf=ImplIntf.VtblImplIntf) and
  2502. assigned(ImplIntf.ProcDefs) then
  2503. begin
  2504. maybe_new_object_file(list);
  2505. for j:=0 to ImplIntf.ProcDefs.Count-1 do
  2506. begin
  2507. pd:=TProcdef(ImplIntf.ProcDefs[j]);
  2508. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2509. ImplIntf.IntfDef.objname^+'_$_'+tostr(j)+'_$_'+pd.mangledname);
  2510. { create wrapper code }
  2511. new_section(list,sec_code,tmps,0);
  2512. cg.init_register_allocators;
  2513. cg.g_intf_wrapper(list,pd,tmps,ImplIntf.ioffset);
  2514. cg.done_register_allocators;
  2515. end;
  2516. end;
  2517. end;
  2518. end;
  2519. procedure gen_intf_wrappers(list:TAsmList;st:TSymtable);
  2520. var
  2521. i : longint;
  2522. def : tdef;
  2523. begin
  2524. for i:=0 to st.DefList.Count-1 do
  2525. begin
  2526. def:=tdef(st.DefList[i]);
  2527. if is_class(def) then
  2528. gen_intf_wrapper(list,tobjectdef(def));
  2529. end;
  2530. end;
  2531. procedure gen_load_vmt_register(list:TAsmList;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2532. var
  2533. href : treference;
  2534. begin
  2535. if is_object(objdef) then
  2536. begin
  2537. case selfloc.loc of
  2538. LOC_CREFERENCE,
  2539. LOC_REFERENCE:
  2540. begin
  2541. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2542. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2543. end;
  2544. else
  2545. internalerror(200305056);
  2546. end;
  2547. end
  2548. else
  2549. begin
  2550. case selfloc.loc of
  2551. LOC_REGISTER:
  2552. begin
  2553. {$ifdef cpu_uses_separate_address_registers}
  2554. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2555. begin
  2556. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2557. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2558. end
  2559. else
  2560. {$endif cpu_uses_separate_address_registers}
  2561. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2562. end;
  2563. LOC_CREGISTER,
  2564. LOC_CREFERENCE,
  2565. LOC_REFERENCE:
  2566. begin
  2567. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2568. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2569. end;
  2570. else
  2571. internalerror(200305057);
  2572. end;
  2573. end;
  2574. vmtreg:=cg.getaddressregister(list);
  2575. cg.g_maybe_testself(list,href.base);
  2576. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2577. { test validity of VMT }
  2578. if not(is_interface(objdef)) and
  2579. not(is_cppclass(objdef)) then
  2580. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2581. end;
  2582. function getprocalign : shortint;
  2583. begin
  2584. { gprof uses 16 byte granularity }
  2585. if (cs_profile in current_settings.moduleswitches) then
  2586. result:=16
  2587. else
  2588. result:=current_settings.alignment.procalign;
  2589. end;
  2590. procedure gen_pic_helpers(list : TAsmList);
  2591. {$ifdef i386}
  2592. var
  2593. href : treference;
  2594. {$endif i386}
  2595. begin
  2596. { if other cpus require such helpers as well, it can be solved more cleanly }
  2597. {$ifdef i386}
  2598. if current_module.requires_ebx_pic_helper then
  2599. begin
  2600. new_section(list,sec_code,'fpc_geteipasebx',0);
  2601. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2602. reference_reset(href);
  2603. href.base:=NR_ESP;
  2604. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2605. list.concat(taicpu.op_none(A_RET,S_NO));
  2606. end;
  2607. if current_module.requires_ecx_pic_helper then
  2608. begin
  2609. new_section(list,sec_code,'fpc_geteipasecx',0);
  2610. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2611. reference_reset(href);
  2612. href.base:=NR_ESP;
  2613. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2614. list.concat(taicpu.op_none(A_RET,S_NO));
  2615. end;
  2616. {$endif i386}
  2617. end;
  2618. end.