ncgutil.pas 120 KB

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