ncgutil.pas 127 KB

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