cgai386.pas 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgai386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cpubase,cpuasm,
  23. symconst,symtype,symdef,aasm;
  24. {$define TESTGETTEMP to store const that
  25. are written into temps for later release PM }
  26. function def_opsize(p1:tdef):topsize;
  27. function def2def_opsize(p1,p2:tdef):topsize;
  28. function def_getreg(p1:tdef):tregister;
  29. function makereg8(r:tregister):tregister;
  30. function makereg16(r:tregister):tregister;
  31. function makereg32(r:tregister):tregister;
  32. procedure locflags2reg(var l:tlocation;opsize:topsize);
  33. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  34. procedure emitlab(var l : tasmlabel);
  35. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  36. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  37. procedure emit_none(i : tasmop;s : topsize);
  38. procedure emit_const(i : tasmop;s : topsize;c : longint);
  39. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  40. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  41. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  42. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  43. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  44. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  45. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  46. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  47. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  48. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  49. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  50. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  51. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  52. procedure emitcall(const routine:string);
  53. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  54. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  55. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  56. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  57. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  58. procedure emit_push_loc(const t:tlocation);
  59. procedure emit_push_mem_size(const t: treference; size: longint);
  60. { pushes qword location to the stack }
  61. procedure emit_pushq_loc(const t : tlocation);
  62. procedure release_qword_loc(const t : tlocation);
  63. { remove non regvar registers in loc from regs (in the format }
  64. { pushusedregisters uses) }
  65. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  66. { releases the registers of a location }
  67. procedure release_loc(const t : tlocation);
  68. procedure emit_pushw_loc(const t:tlocation);
  69. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  70. procedure emit_to_mem(var t:tlocation;def:tdef);
  71. procedure emit_to_reg16(var hr:tregister);
  72. procedure emit_to_reg32(var hr:tregister);
  73. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  74. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  75. procedure copyshortstring(const dref,sref : treference;len : byte;
  76. loadref, del_sref: boolean);
  77. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  78. procedure incrstringref(t : tdef;const ref : treference);
  79. procedure decrstringref(t : tdef;const ref : treference);
  80. procedure push_int(l : longint);
  81. procedure emit_push_mem(const ref : treference);
  82. procedure emitpushreferenceaddr(const ref : treference);
  83. procedure incrcomintfref(t: tdef; const ref: treference);
  84. procedure decrcomintfref(t: tdef; const ref: treference);
  85. procedure floatload(t : tfloattype;const ref : treference);
  86. procedure floatstore(t : tfloattype;const ref : treference);
  87. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  88. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  89. procedure maybe_loadself;
  90. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;destreg:Tregister;delloc:boolean);
  91. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  92. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  93. stackframe:longint;
  94. var parasize:longint;var nostackframe:boolean;
  95. inlined : boolean);
  96. procedure genexitcode(alist : TAAsmoutput;parasize:longint;
  97. nostackframe,inlined:boolean);
  98. { if a unit doesn't have a explicit init/final code, }
  99. { we've to generate one, if the units has ansistrings }
  100. { in the interface or implementation }
  101. procedure genimplicitunitfinal(alist : TAAsmoutput);
  102. procedure genimplicitunitinit(alist : TAAsmoutput);
  103. {$ifdef test_dest_loc}
  104. const
  105. { used to avoid temporary assignments }
  106. dest_loc_known : boolean = false;
  107. in_dest_loc : boolean = false;
  108. dest_loc_tree : ptree = nil;
  109. var
  110. dest_loc : tlocation;
  111. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  112. {$endif test_dest_loc}
  113. implementation
  114. uses
  115. {$ifdef delphi}
  116. sysutils,
  117. {$else}
  118. strings,
  119. {$endif}
  120. cutils,cclasses,
  121. globtype,systems,globals,verbose,
  122. fmodule,
  123. symbase,symsym,symtable,types,
  124. tgcpu,temp_gen,hcodegen,regvars
  125. {$ifdef GDB}
  126. ,gdb
  127. {$endif}
  128. ;
  129. {$ifndef NOTARGETWIN32}
  130. const
  131. winstackpagesize = 4096;
  132. {$endif}
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. function def_opsize(p1:tdef):topsize;
  137. begin
  138. case p1.size of
  139. 1 : def_opsize:=S_B;
  140. 2 : def_opsize:=S_W;
  141. 4 : def_opsize:=S_L;
  142. else
  143. internalerror(130820001);
  144. end;
  145. end;
  146. function def2def_opsize(p1,p2:tdef):topsize;
  147. var
  148. o1 : topsize;
  149. begin
  150. case p1.size of
  151. 1 : o1:=S_B;
  152. 2 : o1:=S_W;
  153. 4 : o1:=S_L;
  154. { I don't know if we need it (FK) }
  155. 8 : o1:=S_L;
  156. else
  157. internalerror(130820002);
  158. end;
  159. if assigned(p2) then
  160. begin
  161. case p2.size of
  162. 1 : o1:=S_B;
  163. 2 : begin
  164. if o1=S_B then
  165. o1:=S_BW
  166. else
  167. o1:=S_W;
  168. end;
  169. 4,8:
  170. begin
  171. case o1 of
  172. S_B : o1:=S_BL;
  173. S_W : o1:=S_WL;
  174. end;
  175. end;
  176. end;
  177. end;
  178. def2def_opsize:=o1;
  179. end;
  180. function def_getreg(p1:tdef):tregister;
  181. begin
  182. case p1.size of
  183. 1 : def_getreg:=reg32toreg8(getregister32);
  184. 2 : def_getreg:=reg32toreg16(getregister32);
  185. 4 : def_getreg:=getregister32;
  186. else
  187. internalerror(130820003);
  188. end;
  189. end;
  190. function makereg8(r:tregister):tregister;
  191. begin
  192. case r of
  193. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  194. makereg8:=reg32toreg8(r);
  195. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  196. makereg8:=reg16toreg8(r);
  197. R_AL,R_BL,R_CL,R_DL :
  198. makereg8:=r;
  199. end;
  200. end;
  201. function makereg16(r:tregister):tregister;
  202. begin
  203. case r of
  204. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  205. makereg16:=reg32toreg16(r);
  206. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  207. makereg16:=r;
  208. R_AL,R_BL,R_CL,R_DL :
  209. makereg16:=reg8toreg16(r);
  210. end;
  211. end;
  212. function makereg32(r:tregister):tregister;
  213. begin
  214. case r of
  215. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  216. makereg32:=r;
  217. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  218. makereg32:=reg16toreg32(r);
  219. R_AL,R_BL,R_CL,R_DL :
  220. makereg32:=reg8toreg32(r);
  221. end;
  222. end;
  223. procedure locflags2reg(var l:tlocation;opsize:topsize);
  224. var
  225. hregister : tregister;
  226. begin
  227. if (l.loc=LOC_FLAGS) then
  228. begin
  229. hregister:=getregister32;
  230. case opsize of
  231. S_W : hregister:=reg32toreg16(hregister);
  232. S_B : hregister:=reg32toreg8(hregister);
  233. end;
  234. emit_flag2reg(l.resflags,hregister);
  235. l.loc:=LOC_REGISTER;
  236. l.register:=hregister;
  237. end
  238. else internalerror(270720001);
  239. end;
  240. procedure locjump2reg(var l:tlocation;opsize:topsize; otl, ofl: tasmlabel);
  241. var
  242. hregister : tregister;
  243. hl : tasmlabel;
  244. begin
  245. if l.loc = LOC_JUMP then
  246. begin
  247. hregister:=getregister32;
  248. case opsize of
  249. S_W : hregister:=reg32toreg16(hregister);
  250. S_B : hregister:=reg32toreg8(hregister);
  251. end;
  252. l.loc:=LOC_REGISTER;
  253. l.register:=hregister;
  254. emitlab(truelabel);
  255. truelabel:=otl;
  256. emit_const_reg(A_MOV,opsize,1,hregister);
  257. getlabel(hl);
  258. emitjmp(C_None,hl);
  259. emitlab(falselabel);
  260. falselabel:=ofl;
  261. emit_reg_reg(A_XOR,S_L,makereg32(hregister),
  262. makereg32(hregister));
  263. emitlab(hl);
  264. end
  265. else internalerror(270720002);
  266. end;
  267. {*****************************************************************************
  268. Emit Assembler
  269. *****************************************************************************}
  270. procedure emitlab(var l : tasmlabel);
  271. begin
  272. if not l.is_set then
  273. exprasmList.concat(Tai_label.Create(l))
  274. else
  275. internalerror(7453984);
  276. end;
  277. procedure emitjmp(c : tasmcond;var l : tasmlabel);
  278. var
  279. ai : taicpu;
  280. begin
  281. if c=C_None then
  282. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  283. else
  284. begin
  285. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  286. ai.SetCondition(c);
  287. end;
  288. ai.is_jmp:=true;
  289. exprasmList.concat(ai);
  290. end;
  291. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  292. var
  293. ai : taicpu;
  294. hreg : tregister;
  295. begin
  296. hreg:=makereg8(hregister);
  297. ai:=Taicpu.Op_reg(A_Setcc,S_B,hreg);
  298. ai.SetCondition(flag_2_cond[flag]);
  299. exprasmList.concat(ai);
  300. if hreg<>hregister then
  301. begin
  302. if hregister in regset16bit then
  303. emit_to_reg16(hreg)
  304. else
  305. emit_to_reg32(hreg);
  306. end;
  307. end;
  308. procedure emit_none(i : tasmop;s : topsize);
  309. begin
  310. exprasmList.concat(Taicpu.Op_none(i,s));
  311. end;
  312. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  313. begin
  314. exprasmList.concat(Taicpu.Op_reg(i,s,reg));
  315. end;
  316. procedure emit_ref(i : tasmop;s : topsize;ref : preference);
  317. begin
  318. exprasmList.concat(Taicpu.Op_ref(i,s,ref));
  319. end;
  320. procedure emit_const(i : tasmop;s : topsize;c : longint);
  321. begin
  322. exprasmList.concat(Taicpu.Op_const(i,s,c));
  323. end;
  324. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  325. begin
  326. exprasmList.concat(Taicpu.Op_const_reg(i,s,c,reg));
  327. end;
  328. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;ref : preference);
  329. begin
  330. exprasmList.concat(Taicpu.Op_const_ref(i,s,c,ref));
  331. end;
  332. procedure emit_ref_reg(i : tasmop;s : topsize;ref : preference;reg : tregister);
  333. begin
  334. exprasmList.concat(Taicpu.Op_ref_reg(i,s,ref,reg));
  335. end;
  336. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;ref : preference);
  337. begin
  338. exprasmList.concat(Taicpu.Op_reg_ref(i,s,reg,ref));
  339. end;
  340. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  341. begin
  342. if (reg1<>reg2) or (i<>A_MOV) then
  343. exprasmList.concat(Taicpu.Op_reg_reg(i,s,reg1,reg2));
  344. end;
  345. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  346. begin
  347. exprasmList.concat(Taicpu.Op_const_reg_reg(i,s,c,reg1,reg2));
  348. end;
  349. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  350. begin
  351. exprasmList.concat(Taicpu.Op_reg_reg_reg(i,s,reg1,reg2,reg3));
  352. end;
  353. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  354. begin
  355. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  356. end;
  357. procedure emit_sym_ofs(i : tasmop;s : topsize;op : tasmsymbol;ofs : longint);
  358. begin
  359. exprasmList.concat(Taicpu.Op_sym_ofs(i,s,op,ofs));
  360. end;
  361. procedure emit_sym_ofs_reg(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;reg : tregister);
  362. begin
  363. exprasmList.concat(Taicpu.Op_sym_ofs_reg(i,s,op,ofs,reg));
  364. end;
  365. procedure emit_sym_ofs_ref(i : tasmop;s : topsize;op : tasmsymbol;ofs:longint;ref : preference);
  366. begin
  367. exprasmList.concat(Taicpu.Op_sym_ofs_ref(i,s,op,ofs,ref));
  368. end;
  369. procedure emitcall(const routine:string);
  370. begin
  371. exprasmList.concat(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  372. end;
  373. { only usefull in startup code }
  374. procedure emitinsertcall(const routine:string);
  375. begin
  376. exprasmList.insert(Taicpu.Op_sym(A_CALL,S_NO,newasmsymbol(routine)));
  377. end;
  378. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference;siz:topsize;freetemp:boolean);
  379. var
  380. hreg : tregister;
  381. pushedeax : boolean;
  382. begin
  383. pushedeax:=false;
  384. case t.loc of
  385. LOC_REGISTER,
  386. LOC_CREGISTER : begin
  387. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  388. t.register,newreference(ref)));
  389. ungetregister32(t.register); { the register is not needed anymore }
  390. end;
  391. LOC_MEM,
  392. LOC_REFERENCE : begin
  393. if t.reference.is_immediate then
  394. emit_const_ref(A_MOV,siz,
  395. t.reference.offset,newreference(ref))
  396. else
  397. begin
  398. case siz of
  399. S_B : begin
  400. { we can't do a getregister in the code generator }
  401. { without problems!!! }
  402. if usablereg32>0 then
  403. hreg:=reg32toreg8(getregister32)
  404. else
  405. begin
  406. emit_reg(A_PUSH,S_L,R_EAX);
  407. pushedeax:=true;
  408. hreg:=R_AL;
  409. end;
  410. end;
  411. S_W : hreg:=R_DI;
  412. S_L : hreg:=R_EDI;
  413. end;
  414. if hreg in [R_DI,R_EDI] then
  415. getexplicitregister32(R_EDI);
  416. emit_ref_reg(A_MOV,siz,
  417. newreference(t.reference),hreg);
  418. del_reference(t.reference);
  419. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,siz,
  420. hreg,newreference(ref)));
  421. if siz=S_B then
  422. begin
  423. if pushedeax then
  424. emit_reg(A_POP,S_L,R_EAX)
  425. else
  426. ungetregister(hreg);
  427. end;
  428. if hreg in [R_DI,R_EDI] then
  429. ungetregister32(R_EDI);
  430. { we can release the registers }
  431. { but only AFTER the MOV! Important for the optimizer!
  432. (JM)}
  433. del_reference(ref);
  434. end;
  435. if freetemp then
  436. ungetiftemp(t.reference);
  437. end;
  438. else
  439. internalerror(330);
  440. end;
  441. end;
  442. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  443. begin
  444. case t.loc of
  445. LOC_REGISTER,
  446. LOC_CREGISTER : begin
  447. emit_reg_reg(A_MOV,S_L,t.register,reg);
  448. ungetregister32(t.register); { the register is not needed anymore }
  449. end;
  450. LOC_MEM,
  451. LOC_REFERENCE : begin
  452. if t.reference.is_immediate then
  453. emit_const_reg(A_MOV,S_L,
  454. t.reference.offset,reg)
  455. else
  456. begin
  457. emit_ref_reg(A_MOV,S_L,
  458. newreference(t.reference),reg);
  459. end;
  460. end;
  461. else
  462. internalerror(330);
  463. end;
  464. end;
  465. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  466. begin
  467. case t.loc of
  468. LOC_REGISTER,
  469. LOC_CREGISTER : begin
  470. emit_reg_reg(A_MOV,RegSize(Reg),
  471. reg,t.register);
  472. end;
  473. LOC_MEM,
  474. LOC_REFERENCE : begin
  475. if t.reference.is_immediate then
  476. internalerror(334)
  477. else
  478. begin
  479. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,RegSize(Reg),
  480. Reg,newreference(t.reference)));
  481. end;
  482. end;
  483. else
  484. internalerror(330);
  485. end;
  486. end;
  487. procedure emit_lea_loc_reg(const t:tlocation;reg:tregister;freetemp:boolean);
  488. begin
  489. case t.loc of
  490. LOC_MEM,
  491. LOC_REFERENCE : begin
  492. if t.reference.is_immediate then
  493. internalerror(331)
  494. else
  495. begin
  496. emit_ref_reg(A_LEA,S_L,
  497. newreference(t.reference),reg);
  498. end;
  499. if freetemp then
  500. ungetiftemp(t.reference);
  501. end;
  502. else
  503. internalerror(332);
  504. end;
  505. end;
  506. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  507. begin
  508. case t.loc of
  509. LOC_REGISTER,
  510. LOC_CREGISTER : begin
  511. emit_reg_reg(A_MOV,S_L,
  512. reglow,t.registerlow);
  513. emit_reg_reg(A_MOV,S_L,
  514. reghigh,t.registerhigh);
  515. end;
  516. LOC_MEM,
  517. LOC_REFERENCE : begin
  518. if t.reference.is_immediate then
  519. internalerror(334)
  520. else
  521. begin
  522. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  523. Reglow,newreference(t.reference)));
  524. inc(t.reference.offset,4);
  525. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  526. Reghigh,newreference(t.reference)));
  527. end;
  528. end;
  529. else
  530. internalerror(330);
  531. end;
  532. end;
  533. procedure emit_pushq_loc(const t : tlocation);
  534. var
  535. hr : preference;
  536. begin
  537. case t.loc of
  538. LOC_REGISTER,
  539. LOC_CREGISTER:
  540. begin
  541. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  542. t.registerhigh));
  543. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,
  544. t.registerlow));
  545. end;
  546. LOC_MEM,
  547. LOC_REFERENCE:
  548. begin
  549. hr:=newreference(t.reference);
  550. inc(hr^.offset,4);
  551. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  552. hr));
  553. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  554. newreference(t.reference)));
  555. ungetiftemp(t.reference);
  556. end;
  557. else internalerror(331);
  558. end;
  559. end;
  560. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: byte);
  561. begin
  562. case t.loc of
  563. LOC_REGISTER:
  564. { can't be a regvar, since it would be LOC_CREGISTER then }
  565. regs := regs and not($80 shr byte(t.register));
  566. LOC_MEM,LOC_REFERENCE:
  567. begin
  568. if not(cs_regalloc in aktglobalswitches) or
  569. (t.reference.base in usableregs) then
  570. regs := regs and
  571. not($80 shr byte(t.reference.base));
  572. if not(cs_regalloc in aktglobalswitches) or
  573. (t.reference.index in usableregs) then
  574. regs := regs and
  575. not($80 shr byte(t.reference.index));
  576. end;
  577. end;
  578. end;
  579. procedure release_loc(const t : tlocation);
  580. begin
  581. case t.loc of
  582. LOC_REGISTER,
  583. LOC_CREGISTER:
  584. begin
  585. ungetregister32(t.register);
  586. end;
  587. LOC_MEM,
  588. LOC_REFERENCE:
  589. del_reference(t.reference);
  590. else internalerror(332);
  591. end;
  592. end;
  593. procedure release_qword_loc(const t : tlocation);
  594. begin
  595. case t.loc of
  596. LOC_REGISTER,
  597. LOC_CREGISTER:
  598. begin
  599. ungetregister32(t.registerhigh);
  600. ungetregister32(t.registerlow);
  601. end;
  602. LOC_MEM,
  603. LOC_REFERENCE:
  604. del_reference(t.reference);
  605. else internalerror(331);
  606. end;
  607. end;
  608. procedure emit_push_loc(const t:tlocation);
  609. begin
  610. case t.loc of
  611. LOC_REGISTER,
  612. LOC_CREGISTER : begin
  613. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)));
  614. ungetregister(t.register); { the register is not needed anymore }
  615. end;
  616. LOC_MEM,
  617. LOC_REFERENCE : begin
  618. if t.reference.is_immediate then
  619. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.reference.offset))
  620. else
  621. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(t.reference)));
  622. del_reference(t.reference);
  623. ungetiftemp(t.reference);
  624. end;
  625. else
  626. internalerror(330);
  627. end;
  628. end;
  629. procedure emit_pushw_loc(const t:tlocation);
  630. var
  631. opsize : topsize;
  632. begin
  633. case t.loc of
  634. LOC_REGISTER,
  635. LOC_CREGISTER : begin
  636. if aktalignment.paraalign=4 then
  637. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,makereg32(t.register)))
  638. else
  639. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,makereg16(t.register)));
  640. ungetregister(t.register); { the register is not needed anymore }
  641. end;
  642. LOC_MEM,
  643. LOC_REFERENCE : begin
  644. if aktalignment.paraalign=4 then
  645. opsize:=S_L
  646. else
  647. opsize:=S_W;
  648. if t.reference.is_immediate then
  649. exprasmList.concat(Taicpu.Op_const(A_PUSH,opsize,t.reference.offset))
  650. else
  651. exprasmList.concat(Taicpu.Op_ref(A_PUSH,opsize,newreference(t.reference)));
  652. del_reference(t.reference);
  653. ungetiftemp(t.reference);
  654. end;
  655. else
  656. internalerror(330);
  657. end;
  658. end;
  659. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference;freetemp:boolean);
  660. begin
  661. case t.loc of
  662. LOC_MEM,
  663. LOC_REFERENCE : begin
  664. if t.reference.is_immediate then
  665. internalerror(331)
  666. else
  667. begin
  668. getexplicitregister32(R_EDI);
  669. emit_ref_reg(A_LEA,S_L,
  670. newreference(t.reference),R_EDI);
  671. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,
  672. R_EDI,newreference(ref)));
  673. ungetregister32(R_EDI);
  674. end;
  675. { release the registers }
  676. del_reference(t.reference);
  677. if freetemp then
  678. ungetiftemp(t.reference);
  679. end;
  680. else
  681. internalerror(332);
  682. end;
  683. end;
  684. procedure emit_push_lea_loc(const t:tlocation;freetemp:boolean);
  685. begin
  686. case t.loc of
  687. LOC_MEM,
  688. LOC_REFERENCE : begin
  689. if t.reference.is_immediate then
  690. internalerror(331)
  691. else
  692. begin
  693. getexplicitregister32(R_EDI);
  694. emit_ref_reg(A_LEA,S_L,
  695. newreference(t.reference),R_EDI);
  696. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  697. ungetregister32(R_EDI);
  698. end;
  699. if freetemp then
  700. ungetiftemp(t.reference);
  701. end;
  702. else
  703. internalerror(332);
  704. end;
  705. end;
  706. procedure emit_push_mem_size(const t: treference; size: longint);
  707. var
  708. s: topsize;
  709. begin
  710. if t.is_immediate then
  711. begin
  712. if (size=4) or
  713. (aktalignment.paraalign=4) then
  714. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,t.offset))
  715. else
  716. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_W,t.offset));
  717. end
  718. else
  719. if size < 4 then
  720. begin
  721. getexplicitregister32(R_EDI);
  722. case size of
  723. 1: s := S_BL;
  724. 2: s := S_WL;
  725. else internalerror(200008071);
  726. end;
  727. exprasmList.concat(Taicpu.Op_ref_reg(A_MOVZX,s,
  728. newreference(t),R_EDI));
  729. if aktalignment.paraalign=4 then
  730. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI))
  731. else
  732. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_W,R_DI));
  733. ungetregister32(R_EDI);
  734. end
  735. else
  736. if size = 4 then
  737. emit_push_mem(t)
  738. else
  739. internalerror(200008072);
  740. end;
  741. procedure emit_to_mem(var t:tlocation;def:tdef);
  742. var
  743. r : treference;
  744. begin
  745. case t.loc of
  746. LOC_FPU : begin
  747. reset_reference(t.reference);
  748. gettempofsizereference(10,t.reference);
  749. floatstore(tfloatdef(def).typ,t.reference);
  750. end;
  751. LOC_REGISTER:
  752. begin
  753. if is_64bitint(def) then
  754. begin
  755. gettempofsizereference(8,r);
  756. emit_reg_ref(A_MOV,S_L,t.registerlow,newreference(r));
  757. inc(r.offset,4);
  758. emit_reg_ref(A_MOV,S_L,t.registerhigh,newreference(r));
  759. dec(r.offset,4);
  760. t.reference:=r;
  761. end
  762. else
  763. internalerror(1405001);
  764. end;
  765. LOC_MEM,
  766. LOC_REFERENCE : ;
  767. LOC_CFPUREGISTER : begin
  768. emit_reg(A_FLD,S_NO,correct_fpuregister(t.register,fpuvaroffset));
  769. inc(fpuvaroffset);
  770. reset_reference(t.reference);
  771. gettempofsizereference(10,t.reference);
  772. floatstore(tfloatdef(def).typ,t.reference);
  773. end;
  774. else
  775. internalerror(333);
  776. end;
  777. t.loc:=LOC_MEM;
  778. end;
  779. procedure emit_to_reg16(var hr:tregister);
  780. begin
  781. { ranges are a little bit bug sensitive ! }
  782. case hr of
  783. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  784. begin
  785. hr:=reg32toreg16(hr);
  786. end;
  787. R_AL,R_BL,R_CL,R_DL:
  788. begin
  789. hr:=reg8toreg16(hr);
  790. emit_const_reg(A_AND,S_W,$ff,hr);
  791. end;
  792. R_AH,R_BH,R_CH,R_DH:
  793. begin
  794. hr:=reg8toreg16(hr);
  795. emit_const_reg(A_AND,S_W,$ff00,hr);
  796. end;
  797. end;
  798. end;
  799. procedure emit_to_reg32(var hr:tregister);
  800. begin
  801. { ranges are a little bit bug sensitive ! }
  802. case hr of
  803. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  804. begin
  805. hr:=reg16toreg32(hr);
  806. emit_const_reg(A_AND,S_L,$ffff,hr);
  807. end;
  808. R_AL,R_BL,R_CL,R_DL:
  809. begin
  810. hr:=reg8toreg32(hr);
  811. emit_const_reg(A_AND,S_L,$ff,hr);
  812. end;
  813. R_AH,R_BH,R_CH,R_DH:
  814. begin
  815. hr:=reg8toreg32(hr);
  816. emit_const_reg(A_AND,S_L,$ff00,hr);
  817. end;
  818. end;
  819. end;
  820. procedure emit_mov_ref_reg64(r : treference;rl,rh : tregister);
  821. var
  822. hr : preference;
  823. begin
  824. { if we load a 64 bit reference, we must be careful because }
  825. { we could overwrite the registers of the reference by }
  826. { accident }
  827. getexplicitregister32(R_EDI);
  828. if r.base=rl then
  829. begin
  830. emit_reg_reg(A_MOV,S_L,r.base,
  831. R_EDI);
  832. r.base:=R_EDI;
  833. end
  834. else if r.index=rl then
  835. begin
  836. emit_reg_reg(A_MOV,S_L,r.index,
  837. R_EDI);
  838. r.index:=R_EDI;
  839. end;
  840. emit_ref_reg(A_MOV,S_L,
  841. newreference(r),rl);
  842. hr:=newreference(r);
  843. inc(hr^.offset,4);
  844. emit_ref_reg(A_MOV,S_L,
  845. hr,rh);
  846. ungetregister32(R_EDI);
  847. end;
  848. {*****************************************************************************
  849. Emit String Functions
  850. *****************************************************************************}
  851. procedure incrcomintfref(t: tdef; const ref: treference);
  852. var
  853. pushedregs : tpushed;
  854. begin
  855. pushusedregisters(pushedregs,$ff);
  856. emit_ref(A_PUSH,S_L,newreference(ref));
  857. saveregvars($ff);
  858. if is_interfacecom(t) then
  859. emitcall('FPC_INTF_INCR_REF')
  860. else
  861. internalerror(1859);
  862. popusedregisters(pushedregs);
  863. end;
  864. procedure decrcomintfref(t: tdef; const ref: treference);
  865. var
  866. pushedregs : tpushed;
  867. begin
  868. pushusedregisters(pushedregs,$ff);
  869. emitpushreferenceaddr(ref);
  870. saveregvars($ff);
  871. if is_interfacecom(t) then
  872. begin
  873. emitcall('FPC_INTF_DECR_REF');
  874. end
  875. else internalerror(1859);
  876. popusedregisters(pushedregs);
  877. end;
  878. procedure copyshortstring(const dref,sref : treference;len : byte;
  879. loadref, del_sref: boolean);
  880. begin
  881. emitpushreferenceaddr(dref);
  882. { if it's deleted right before it's used, the optimizer can move }
  883. { the reg deallocations to the right places (JM) }
  884. if del_sref then
  885. del_reference(sref);
  886. if loadref then
  887. emit_push_mem(sref)
  888. else
  889. emitpushreferenceaddr(sref);
  890. push_int(len);
  891. emitcall('FPC_SHORTSTR_COPY');
  892. maybe_loadself;
  893. end;
  894. {$ifdef unused}
  895. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  896. begin
  897. emitpushreferenceaddr(dref);
  898. if loadref then
  899. emit_push_mem(sref)
  900. else
  901. emitpushreferenceaddr(sref);
  902. push_int(len);
  903. saveregvars($ff);
  904. emitcall('FPC_LONGSTR_COPY');
  905. maybe_loadself;
  906. end;
  907. {$endif unused}
  908. procedure incrstringref(t : tdef;const ref : treference);
  909. var
  910. pushedregs : tpushed;
  911. begin
  912. pushusedregisters(pushedregs,$ff);
  913. emitpushreferenceaddr(ref);
  914. saveregvars($ff);
  915. if is_ansistring(t) then
  916. begin
  917. emitcall('FPC_ANSISTR_INCR_REF');
  918. end
  919. else if is_widestring(t) then
  920. begin
  921. emitcall('FPC_WIDESTR_INCR_REF');
  922. end
  923. else internalerror(1859);
  924. popusedregisters(pushedregs);
  925. end;
  926. procedure decrstringref(t : tdef;const ref : treference);
  927. var
  928. pushedregs : tpushed;
  929. begin
  930. pushusedregisters(pushedregs,$ff);
  931. emitpushreferenceaddr(ref);
  932. saveregvars($ff);
  933. if is_ansistring(t) then
  934. begin
  935. emitcall('FPC_ANSISTR_DECR_REF');
  936. end
  937. else if is_widestring(t) then
  938. begin
  939. emitcall('FPC_WIDESTR_DECR_REF');
  940. end
  941. else internalerror(1859);
  942. popusedregisters(pushedregs);
  943. end;
  944. {*****************************************************************************
  945. Emit Push Functions
  946. *****************************************************************************}
  947. procedure push_int(l : longint);
  948. begin
  949. if (l = 0) and
  950. not(aktoptprocessor in [Class386, ClassP6]) and
  951. not(cs_littlesize in aktglobalswitches)
  952. Then
  953. begin
  954. getexplicitregister32(R_EDI);
  955. emit_reg_reg(A_XOR,S_L,R_EDI,R_EDI);
  956. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  957. ungetregister32(R_EDI);
  958. end
  959. else
  960. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,l));
  961. end;
  962. procedure emit_push_mem(const ref : treference);
  963. begin
  964. if ref.is_immediate then
  965. push_int(ref.offset)
  966. else
  967. begin
  968. if not(aktoptprocessor in [Class386, ClassP6]) and
  969. not(cs_littlesize in aktglobalswitches)
  970. then
  971. begin
  972. getexplicitregister32(R_EDI);
  973. emit_ref_reg(A_MOV,S_L,newreference(ref),R_EDI);
  974. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  975. ungetregister32(R_EDI);
  976. end
  977. else exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,newreference(ref)));
  978. end;
  979. end;
  980. procedure emitpushreferenceaddr(const ref : treference);
  981. var
  982. href : treference;
  983. begin
  984. { this will fail for references to other segments !!! }
  985. if ref.is_immediate then
  986. { is this right ? }
  987. begin
  988. { push_int(ref.offset)}
  989. gettempofsizereference(4,href);
  990. emit_const_ref(A_MOV,S_L,ref.offset,newreference(href));
  991. emitpushreferenceaddr(href);
  992. del_reference(href);
  993. end
  994. else
  995. begin
  996. if ref.segment<>R_NO then
  997. CGMessage(cg_e_cant_use_far_pointer_there);
  998. if (ref.base=R_NO) and (ref.index=R_NO) then
  999. exprasmList.concat(Taicpu.Op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset))
  1000. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  1001. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  1002. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.index))
  1003. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  1004. (ref.offset=0) and (ref.symbol=nil) then
  1005. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,ref.base))
  1006. else
  1007. begin
  1008. getexplicitregister32(R_EDI);
  1009. emit_ref_reg(A_LEA,S_L,newreference(ref),R_EDI);
  1010. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1011. ungetregister32(R_EDI);
  1012. end;
  1013. end;
  1014. end;
  1015. {*****************************************************************************
  1016. Emit Float Functions
  1017. *****************************************************************************}
  1018. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1019. begin
  1020. case t of
  1021. s32real : begin
  1022. op:=A_FLD;
  1023. s:=S_FS;
  1024. end;
  1025. s64real : begin
  1026. op:=A_FLD;
  1027. { ???? }
  1028. s:=S_FL;
  1029. end;
  1030. s80real : begin
  1031. op:=A_FLD;
  1032. s:=S_FX;
  1033. end;
  1034. s64comp : begin
  1035. op:=A_FILD;
  1036. s:=S_IQ;
  1037. end;
  1038. else internalerror(17);
  1039. end;
  1040. end;
  1041. procedure floatload(t : tfloattype;const ref : treference);
  1042. var
  1043. op : tasmop;
  1044. s : topsize;
  1045. begin
  1046. floatloadops(t,op,s);
  1047. exprasmList.concat(Taicpu.Op_ref(op,s,
  1048. newreference(ref)));
  1049. inc(fpuvaroffset);
  1050. end;
  1051. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1052. begin
  1053. case t of
  1054. s32real : begin
  1055. op:=A_FSTP;
  1056. s:=S_FS;
  1057. end;
  1058. s64real : begin
  1059. op:=A_FSTP;
  1060. s:=S_FL;
  1061. end;
  1062. s80real : begin
  1063. op:=A_FSTP;
  1064. s:=S_FX;
  1065. end;
  1066. s64comp : begin
  1067. op:=A_FISTP;
  1068. s:=S_IQ;
  1069. end;
  1070. else
  1071. internalerror(17);
  1072. end;
  1073. end;
  1074. procedure floatstore(t : tfloattype;const ref : treference);
  1075. var
  1076. op : tasmop;
  1077. s : topsize;
  1078. begin
  1079. floatstoreops(t,op,s);
  1080. exprasmList.concat(Taicpu.Op_ref(op,s,
  1081. newreference(ref)));
  1082. dec(fpuvaroffset);
  1083. end;
  1084. {*****************************************************************************
  1085. Emit Functions
  1086. *****************************************************************************}
  1087. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1088. const
  1089. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1090. ishr : array[0..3] of byte=(2,0,1,0);
  1091. var
  1092. ecxpushed : boolean;
  1093. helpsize : longint;
  1094. i : byte;
  1095. reg8,reg32 : tregister;
  1096. swap : boolean;
  1097. procedure maybepushecx;
  1098. begin
  1099. if not(R_ECX in unused) then
  1100. begin
  1101. exprasmList.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1102. ecxpushed:=true;
  1103. end
  1104. else getexplicitregister32(R_ECX);
  1105. end;
  1106. begin
  1107. {$IfNDef regallocfix}
  1108. If delsource then
  1109. del_reference(source);
  1110. {$EndIf regallocfix}
  1111. if (not loadref) and
  1112. ((size<=8) or
  1113. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1114. begin
  1115. helpsize:=size shr 2;
  1116. getexplicitregister32(R_EDI);
  1117. for i:=1 to helpsize do
  1118. begin
  1119. emit_ref_reg(A_MOV,S_L,newreference(source),R_EDI);
  1120. {$ifdef regallocfix}
  1121. If (size = 4) and delsource then
  1122. del_reference(source);
  1123. {$endif regallocfix}
  1124. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest)));
  1125. inc(source.offset,4);
  1126. inc(dest.offset,4);
  1127. dec(size,4);
  1128. end;
  1129. if size>1 then
  1130. begin
  1131. emit_ref_reg(A_MOV,S_W,newreference(source),R_DI);
  1132. {$ifdef regallocfix}
  1133. If (size = 2) and delsource then
  1134. del_reference(source);
  1135. {$endif regallocfix}
  1136. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_W,R_DI,newreference(dest)));
  1137. inc(source.offset,2);
  1138. inc(dest.offset,2);
  1139. dec(size,2);
  1140. end;
  1141. ungetregister32(R_EDI);
  1142. if size>0 then
  1143. begin
  1144. { and now look for an 8 bit register }
  1145. swap:=false;
  1146. if R_EAX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EAX))
  1147. else if R_EDX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EDX))
  1148. else if R_EBX in unused then reg8:=reg32toreg8(getexplicitregister32(R_EBX))
  1149. else if R_ECX in unused then reg8:=reg32toreg8(getexplicitregister32(R_ECX))
  1150. else
  1151. begin
  1152. swap:=true;
  1153. { we need only to check 3 registers, because }
  1154. { one is always not index or base }
  1155. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1156. begin
  1157. reg8:=R_AL;
  1158. reg32:=R_EAX;
  1159. end
  1160. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1161. begin
  1162. reg8:=R_BL;
  1163. reg32:=R_EBX;
  1164. end
  1165. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1166. begin
  1167. reg8:=R_CL;
  1168. reg32:=R_ECX;
  1169. end;
  1170. end;
  1171. if swap then
  1172. { was earlier XCHG, of course nonsense }
  1173. begin
  1174. getexplicitregister32(R_EDI);
  1175. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1176. end;
  1177. emit_ref_reg(A_MOV,S_B,newreference(source),reg8);
  1178. {$ifdef regallocfix}
  1179. If delsource then
  1180. del_reference(source);
  1181. {$endif regallocfix}
  1182. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_B,reg8,newreference(dest)));
  1183. if swap then
  1184. begin
  1185. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1186. ungetregister32(R_EDI);
  1187. end
  1188. else
  1189. ungetregister(reg8);
  1190. end;
  1191. end
  1192. else
  1193. begin
  1194. getexplicitregister32(R_EDI);
  1195. emit_ref_reg(A_LEA,S_L,newreference(dest),R_EDI);
  1196. {$ifdef regallocfix}
  1197. {is this ok?? (JM)}
  1198. del_reference(dest);
  1199. {$endif regallocfix}
  1200. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1201. if loadref then
  1202. emit_ref_reg(A_MOV,S_L,newreference(source),R_ESI)
  1203. else
  1204. begin
  1205. emit_ref_reg(A_LEA,S_L,newreference(source),R_ESI);
  1206. {$ifdef regallocfix}
  1207. if delsource then
  1208. del_reference(source);
  1209. {$endif regallocfix}
  1210. end;
  1211. exprasmList.concat(Taicpu.Op_none(A_CLD,S_NO));
  1212. ecxpushed:=false;
  1213. if cs_littlesize in aktglobalswitches then
  1214. begin
  1215. maybepushecx;
  1216. emit_const_reg(A_MOV,S_L,size,R_ECX);
  1217. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1218. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1219. end
  1220. else
  1221. begin
  1222. helpsize:=size shr 2;
  1223. size:=size and 3;
  1224. if helpsize>1 then
  1225. begin
  1226. maybepushecx;
  1227. emit_const_reg(A_MOV,S_L,helpsize,R_ECX);
  1228. exprasmList.concat(Taicpu.Op_none(A_REP,S_NO));
  1229. end;
  1230. if helpsize>0 then
  1231. exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1232. if size>1 then
  1233. begin
  1234. dec(size,2);
  1235. exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1236. end;
  1237. if size=1 then
  1238. exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1239. end;
  1240. ungetregister32(R_EDI);
  1241. exprasmList.concat(Tairegalloc.DeAlloc(R_ESI));
  1242. if ecxpushed then
  1243. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX))
  1244. else
  1245. ungetregister32(R_ECX);
  1246. { loading SELF-reference again }
  1247. maybe_loadself;
  1248. end;
  1249. if delsource then
  1250. ungetiftemp(source);
  1251. end;
  1252. procedure emitloadord2reg(const location:Tlocation;orddef:torddef;
  1253. destreg:Tregister;delloc:boolean);
  1254. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1255. var tai:Taicpu;
  1256. r:Preference;
  1257. begin
  1258. tai := nil;
  1259. case location.loc of
  1260. LOC_REGISTER,LOC_CREGISTER:
  1261. begin
  1262. case orddef.typ of
  1263. u8bit,uchar,bool8bit:
  1264. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_BL,location.register,destreg);
  1265. s8bit:
  1266. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_BL,location.register,destreg);
  1267. u16bit,uwidechar,bool16bit:
  1268. tai:=Taicpu.Op_reg_reg(A_MOVZX,S_WL,location.register,destreg);
  1269. s16bit:
  1270. tai:=Taicpu.Op_reg_reg(A_MOVSX,S_WL,location.register,destreg);
  1271. u32bit,bool32bit,s32bit:
  1272. if location.register <> destreg then
  1273. tai:=Taicpu.Op_reg_reg(A_MOV,S_L,location.register,destreg);
  1274. else
  1275. internalerror(330);
  1276. end;
  1277. if delloc then
  1278. ungetregister(location.register);
  1279. end;
  1280. LOC_MEM,
  1281. LOC_REFERENCE:
  1282. begin
  1283. if location.reference.is_immediate then
  1284. tai:=Taicpu.Op_const_reg(A_MOV,S_L,location.reference.offset,destreg)
  1285. else
  1286. begin
  1287. r:=newreference(location.reference);
  1288. case orddef.typ of
  1289. u8bit,uchar,bool8bit:
  1290. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_BL,r,destreg);
  1291. s8bit:
  1292. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_BL,r,destreg);
  1293. u16bit,uwidechar,bool16bit:
  1294. tai:=Taicpu.Op_ref_reg(A_MOVZX,S_WL,r,destreg);
  1295. s16bit:
  1296. tai:=Taicpu.Op_ref_reg(A_MOVSX,S_WL,r,destreg);
  1297. u32bit,bool32bit:
  1298. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1299. s32bit:
  1300. tai:=Taicpu.Op_ref_reg(A_MOV,S_L,r,destreg);
  1301. else
  1302. internalerror(330);
  1303. end;
  1304. end;
  1305. if delloc then
  1306. del_reference(location.reference);
  1307. end
  1308. else
  1309. internalerror(6);
  1310. end;
  1311. if assigned(tai) then
  1312. exprasmList.concat(tai);
  1313. end;
  1314. { if necessary ESI is reloaded after a call}
  1315. procedure maybe_loadself;
  1316. var
  1317. hp : preference;
  1318. p : pprocinfo;
  1319. i : longint;
  1320. begin
  1321. if assigned(procinfo^._class) then
  1322. begin
  1323. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1324. if lexlevel>normal_function_level then
  1325. begin
  1326. new(hp);
  1327. reset_reference(hp^);
  1328. hp^.offset:=procinfo^.framepointer_offset;
  1329. hp^.base:=procinfo^.framepointer;
  1330. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1331. p:=procinfo^.parent;
  1332. for i:=3 to lexlevel-1 do
  1333. begin
  1334. new(hp);
  1335. reset_reference(hp^);
  1336. hp^.offset:=p^.framepointer_offset;
  1337. hp^.base:=R_ESI;
  1338. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1339. p:=p^.parent;
  1340. end;
  1341. new(hp);
  1342. reset_reference(hp^);
  1343. hp^.offset:=p^.selfpointer_offset;
  1344. hp^.base:=R_ESI;
  1345. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1346. end
  1347. else
  1348. begin
  1349. new(hp);
  1350. reset_reference(hp^);
  1351. hp^.offset:=procinfo^.selfpointer_offset;
  1352. hp^.base:=procinfo^.framepointer;
  1353. emit_ref_reg(A_MOV,S_L,hp,R_ESI);
  1354. end;
  1355. end;
  1356. end;
  1357. {*****************************************************************************
  1358. Entry/Exit Code Functions
  1359. *****************************************************************************}
  1360. procedure genprofilecode;
  1361. var
  1362. pl : tasmlabel;
  1363. begin
  1364. if (po_assembler in aktprocsym.definition.procoptions) then
  1365. exit;
  1366. case target_info.target of
  1367. target_i386_freebsd,
  1368. target_i386_linux:
  1369. begin
  1370. getaddrlabel(pl);
  1371. emitinsertcall('mcount');
  1372. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  1373. exprasmList.insert(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX));
  1374. exprasmList.insert(Tai_section.Create(sec_code));
  1375. exprasmList.insert(Tai_const.Create_32bit(0));
  1376. exprasmList.insert(Tai_label.Create(pl));
  1377. exprasmList.insert(Tai_align.Create(4));
  1378. exprasmList.insert(Tai_section.Create(sec_data));
  1379. end;
  1380. target_i386_go32v2:
  1381. begin
  1382. emitinsertcall('MCOUNT');
  1383. end;
  1384. end;
  1385. end;
  1386. procedure generate_interrupt_stackframe_entry;
  1387. begin
  1388. { save the registers of an interrupt procedure }
  1389. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EAX));
  1390. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1391. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  1392. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDX));
  1393. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1394. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1395. { .... also the segment registers }
  1396. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_DS));
  1397. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_ES));
  1398. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_FS));
  1399. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_W,R_GS));
  1400. end;
  1401. procedure generate_interrupt_stackframe_exit;
  1402. begin
  1403. { restore the registers of an interrupt procedure }
  1404. { this was all with entrycode instead of exitcode !!}
  1405. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EAX));
  1406. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  1407. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ECX));
  1408. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDX));
  1409. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  1410. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  1411. { .... also the segment registers }
  1412. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_DS));
  1413. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_ES));
  1414. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_FS));
  1415. procinfo^.aktexitcode.concat(Taicpu.Op_reg(A_POP,S_W,R_GS));
  1416. { this restores the flags }
  1417. procinfo^.aktexitcode.concat(Taicpu.Op_none(A_IRET,S_NO));
  1418. end;
  1419. { generates the code for threadvar initialisation }
  1420. procedure initialize_threadvar(p : tnamedindexitem);
  1421. var
  1422. hr : treference;
  1423. begin
  1424. if (tsym(p).typ=varsym) and
  1425. (vo_is_thread_var in tvarsym(p).varoptions) then
  1426. begin
  1427. exprasmList.concat(Taicpu.Op_const(A_PUSH,S_L,tvarsym(p).getsize));
  1428. reset_reference(hr);
  1429. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1430. emitpushreferenceaddr(hr);
  1431. saveregvars($ff);
  1432. emitcall('FPC_INIT_THREADVAR');
  1433. end;
  1434. end;
  1435. { initilizes data of type t }
  1436. { if is_already_ref is true then the routines assumes }
  1437. { that r points to the data to initialize }
  1438. procedure initialize(t : tdef;const ref : treference;is_already_ref : boolean);
  1439. var
  1440. hr : treference;
  1441. begin
  1442. if is_ansistring(t) or
  1443. is_widestring(t) or
  1444. is_interfacecom(t) then
  1445. begin
  1446. emit_const_ref(A_MOV,S_L,0,
  1447. newreference(ref));
  1448. end
  1449. else
  1450. begin
  1451. reset_reference(hr);
  1452. hr.symbol:=tstoreddef(t).get_inittable_label;
  1453. emitpushreferenceaddr(hr);
  1454. if is_already_ref then
  1455. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1456. newreference(ref)))
  1457. else
  1458. emitpushreferenceaddr(ref);
  1459. emitcall('FPC_INITIALIZE');
  1460. end;
  1461. end;
  1462. { finalizes data of type t }
  1463. { if is_already_ref is true then the routines assumes }
  1464. { that r points to the data to finalizes }
  1465. procedure finalize(t : tdef;const ref : treference;is_already_ref : boolean);
  1466. var
  1467. r : treference;
  1468. begin
  1469. if is_ansistring(t) or
  1470. is_widestring(t) then
  1471. begin
  1472. decrstringref(t,ref);
  1473. end
  1474. else if is_interfacecom(t) then
  1475. begin
  1476. decrcomintfref(t,ref);
  1477. end
  1478. else
  1479. begin
  1480. reset_reference(r);
  1481. r.symbol:=tstoreddef(t).get_inittable_label;
  1482. emitpushreferenceaddr(r);
  1483. if is_already_ref then
  1484. exprasmList.concat(Taicpu.Op_ref(A_PUSH,S_L,
  1485. newreference(ref)))
  1486. else
  1487. emitpushreferenceaddr(ref);
  1488. emitcall('FPC_FINALIZE');
  1489. end;
  1490. end;
  1491. { generates the code for initialisation of local data }
  1492. procedure initialize_data(p : tnamedindexitem);
  1493. var
  1494. hr : treference;
  1495. begin
  1496. if (tsym(p).typ=varsym) and
  1497. assigned(tvarsym(p).vartype.def) and
  1498. not(is_class(tvarsym(p).vartype.def)) and
  1499. tvarsym(p).vartype.def.needs_inittable then
  1500. begin
  1501. if assigned(procinfo) then
  1502. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1503. reset_reference(hr);
  1504. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  1505. begin
  1506. hr.base:=procinfo^.framepointer;
  1507. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1508. end
  1509. else
  1510. begin
  1511. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1512. end;
  1513. initialize(tvarsym(p).vartype.def,hr,false);
  1514. end;
  1515. end;
  1516. { generates the code for incrementing the reference count of parameters and
  1517. initialize out parameters }
  1518. procedure init_paras(p : tnamedindexitem);
  1519. var
  1520. hrv : treference;
  1521. hr: treference;
  1522. begin
  1523. if (tsym(p).typ=varsym) and
  1524. not is_class(tvarsym(p).vartype.def) and
  1525. tvarsym(p).vartype.def.needs_inittable then
  1526. begin
  1527. if (tvarsym(p).varspez=vs_value) then
  1528. begin
  1529. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1530. reset_reference(hrv);
  1531. hrv.base:=procinfo^.framepointer;
  1532. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1533. if is_ansistring(tvarsym(p).vartype.def) or
  1534. is_widestring(tvarsym(p).vartype.def) then
  1535. begin
  1536. incrstringref(tvarsym(p).vartype.def,hrv)
  1537. end
  1538. else if is_interfacecom(tvarsym(p).vartype.def) then
  1539. begin
  1540. incrcomintfref(tvarsym(p).vartype.def,hrv)
  1541. end
  1542. else
  1543. begin
  1544. reset_reference(hr);
  1545. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1546. emitpushreferenceaddr(hr);
  1547. emitpushreferenceaddr(hrv);
  1548. emitcall('FPC_ADDREF');
  1549. end;
  1550. end
  1551. else if (tvarsym(p).varspez=vs_out) then
  1552. begin
  1553. reset_reference(hrv);
  1554. hrv.base:=procinfo^.framepointer;
  1555. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1556. getexplicitregister32(R_EDI);
  1557. exprasmList.concat(Taicpu.Op_ref_reg(A_MOV,S_L,newreference(hrv),R_EDI));
  1558. reset_reference(hr);
  1559. hr.base:=R_EDI;
  1560. initialize(tvarsym(p).vartype.def,hr,false);
  1561. end;
  1562. end;
  1563. end;
  1564. { generates the code for decrementing the reference count of parameters }
  1565. procedure final_paras(p : tnamedindexitem);
  1566. var
  1567. hrv : treference;
  1568. hr: treference;
  1569. begin
  1570. if (tsym(p).typ=varsym) and
  1571. not is_class(tvarsym(p).vartype.def) and
  1572. tvarsym(p).vartype.def.needs_inittable then
  1573. begin
  1574. if (tvarsym(p).varspez=vs_value) then
  1575. begin
  1576. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1577. reset_reference(hrv);
  1578. hrv.base:=procinfo^.framepointer;
  1579. hrv.offset:=tvarsym(p).address+procinfo^.para_offset;
  1580. if is_ansistring(tvarsym(p).vartype.def) or
  1581. is_widestring(tvarsym(p).vartype.def) then
  1582. begin
  1583. decrstringref(tvarsym(p).vartype.def,hrv)
  1584. end
  1585. else if is_interfacecom(tvarsym(p).vartype.def) then
  1586. begin
  1587. decrcomintfref(tvarsym(p).vartype.def,hrv)
  1588. end
  1589. else
  1590. begin
  1591. reset_reference(hr);
  1592. hr.symbol:=tstoreddef(tvarsym(p).vartype.def).get_inittable_label;
  1593. emitpushreferenceaddr(hr);
  1594. emitpushreferenceaddr(hrv);
  1595. emitcall('FPC_DECREF');
  1596. end;
  1597. end;
  1598. end;
  1599. end;
  1600. { generates the code for finalisation of local data }
  1601. procedure finalize_data(p : tnamedindexitem);
  1602. var
  1603. hr : treference;
  1604. begin
  1605. if (tsym(p).typ=varsym) and
  1606. assigned(tvarsym(p).vartype.def) and
  1607. not(is_class(tvarsym(p).vartype.def)) and
  1608. tvarsym(p).vartype.def.needs_inittable then
  1609. begin
  1610. if assigned(procinfo) then
  1611. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1612. reset_reference(hr);
  1613. case tsym(p).owner.symtabletype of
  1614. localsymtable,inlinelocalsymtable:
  1615. begin
  1616. hr.base:=procinfo^.framepointer;
  1617. hr.offset:=-tvarsym(p).address+tvarsym(p).owner.address_fixup;
  1618. end;
  1619. else
  1620. hr.symbol:=newasmsymbol(tvarsym(p).mangledname);
  1621. end;
  1622. finalize(tvarsym(p).vartype.def,hr,false);
  1623. end;
  1624. end;
  1625. { generates the code to make local copies of the value parameters }
  1626. procedure copyvalueparas(p : tnamedindexitem);
  1627. var
  1628. href1,href2 : treference;
  1629. r : preference;
  1630. power,len : longint;
  1631. opsize : topsize;
  1632. {$ifndef NOTARGETWIN32}
  1633. again,ok : tasmlabel;
  1634. {$endif}
  1635. begin
  1636. if (tsym(p).typ=varsym) and
  1637. (tvarsym(p).varspez=vs_value) and
  1638. (push_addr_param(tvarsym(p).vartype.def)) then
  1639. begin
  1640. if is_open_array(tvarsym(p).vartype.def) or
  1641. is_array_of_const(tvarsym(p).vartype.def) then
  1642. begin
  1643. { get stack space }
  1644. new(r);
  1645. reset_reference(r^);
  1646. r^.base:=procinfo^.framepointer;
  1647. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1648. getexplicitregister32(R_EDI);
  1649. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1650. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1651. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1652. begin
  1653. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1654. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1655. else
  1656. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1657. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1658. end;
  1659. {$ifndef NOTARGETWIN32}
  1660. { windows guards only a few pages for stack growing, }
  1661. { so we have to access every page first }
  1662. if target_info.target=target_i386_win32 then
  1663. begin
  1664. getlabel(again);
  1665. getlabel(ok);
  1666. emitlab(again);
  1667. exprasmList.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI));
  1668. emitjmp(C_C,ok);
  1669. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  1670. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  1671. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI));
  1672. emitjmp(C_None,again);
  1673. emitlab(ok);
  1674. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1675. ungetregister32(R_EDI);
  1676. { now reload EDI }
  1677. new(r);
  1678. reset_reference(r^);
  1679. r^.base:=procinfo^.framepointer;
  1680. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1681. getexplicitregister32(R_EDI);
  1682. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_EDI));
  1683. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_EDI));
  1684. if (tarraydef(tvarsym(p).vartype.def).elesize<>1) then
  1685. begin
  1686. if ispowerof2(tarraydef(tvarsym(p).vartype.def).elesize, power) then
  1687. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_EDI))
  1688. else
  1689. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,
  1690. tarraydef(tvarsym(p).vartype.def).elesize,R_EDI));
  1691. end;
  1692. end
  1693. else
  1694. {$endif NOTARGETWIN32}
  1695. exprasmList.concat(Taicpu.op_reg_reg(A_SUB,S_L,R_EDI,R_ESP));
  1696. { load destination }
  1697. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  1698. { don't destroy the registers! }
  1699. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ECX));
  1700. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_ESI));
  1701. { load count }
  1702. new(r);
  1703. reset_reference(r^);
  1704. r^.base:=procinfo^.framepointer;
  1705. r^.offset:=tvarsym(p).address+4+procinfo^.para_offset;
  1706. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ECX));
  1707. { load source }
  1708. new(r);
  1709. reset_reference(r^);
  1710. r^.base:=procinfo^.framepointer;
  1711. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1712. exprasmList.concat(Taicpu.op_ref_reg(A_MOV,S_L,r,R_ESI));
  1713. { scheduled .... }
  1714. exprasmList.concat(Taicpu.op_reg(A_INC,S_L,R_ECX));
  1715. { calculate size }
  1716. len:=tarraydef(tvarsym(p).vartype.def).elesize;
  1717. opsize:=S_B;
  1718. if (len and 3)=0 then
  1719. begin
  1720. opsize:=S_L;
  1721. len:=len shr 2;
  1722. end
  1723. else
  1724. if (len and 1)=0 then
  1725. begin
  1726. opsize:=S_W;
  1727. len:=len shr 1;
  1728. end;
  1729. if ispowerof2(len, power) then
  1730. exprasmList.concat(Taicpu.op_const_reg(A_SHL,S_L,power,R_ECX))
  1731. else
  1732. exprasmList.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,R_ECX));
  1733. exprasmList.concat(Taicpu.op_none(A_REP,S_NO));
  1734. case opsize of
  1735. S_B : exprasmList.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1736. S_W : exprasmList.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1737. S_L : exprasmList.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1738. end;
  1739. ungetregister32(R_EDI);
  1740. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ESI));
  1741. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_ECX));
  1742. { patch the new address }
  1743. new(r);
  1744. reset_reference(r^);
  1745. r^.base:=procinfo^.framepointer;
  1746. r^.offset:=tvarsym(p).address+procinfo^.para_offset;
  1747. exprasmList.concat(Taicpu.op_reg_ref(A_MOV,S_L,R_ESP,r));
  1748. end
  1749. else
  1750. if is_shortstring(tvarsym(p).vartype.def) then
  1751. begin
  1752. reset_reference(href1);
  1753. href1.base:=procinfo^.framepointer;
  1754. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1755. reset_reference(href2);
  1756. href2.base:=procinfo^.framepointer;
  1757. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1758. copyshortstring(href2,href1,tstringdef(tvarsym(p).vartype.def).len,true,false);
  1759. end
  1760. else
  1761. begin
  1762. reset_reference(href1);
  1763. href1.base:=procinfo^.framepointer;
  1764. href1.offset:=tvarsym(p).address+procinfo^.para_offset;
  1765. reset_reference(href2);
  1766. href2.base:=procinfo^.framepointer;
  1767. href2.offset:=-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup;
  1768. concatcopy(href1,href2,tvarsym(p).vartype.def.size,true,true);
  1769. end;
  1770. end;
  1771. end;
  1772. procedure inittempvariables;
  1773. var
  1774. hp : ptemprecord;
  1775. r : preference;
  1776. begin
  1777. hp:=templist;
  1778. while assigned(hp) do
  1779. begin
  1780. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1781. tt_widestring,tt_freewidestring,
  1782. tt_interfacecom] then
  1783. begin
  1784. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1785. new(r);
  1786. reset_reference(r^);
  1787. r^.base:=procinfo^.framepointer;
  1788. r^.offset:=hp^.pos;
  1789. emit_const_ref(A_MOV,S_L,0,r);
  1790. end;
  1791. hp:=hp^.next;
  1792. end;
  1793. end;
  1794. procedure finalizetempvariables;
  1795. var
  1796. hp : ptemprecord;
  1797. hr : treference;
  1798. begin
  1799. hp:=templist;
  1800. while assigned(hp) do
  1801. begin
  1802. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  1803. begin
  1804. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1805. reset_reference(hr);
  1806. hr.base:=procinfo^.framepointer;
  1807. hr.offset:=hp^.pos;
  1808. emitpushreferenceaddr(hr);
  1809. emitcall('FPC_ANSISTR_DECR_REF');
  1810. end
  1811. else if hp^.temptype in [tt_widestring,tt_freewidestring] then
  1812. begin
  1813. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1814. reset_reference(hr);
  1815. hr.base:=procinfo^.framepointer;
  1816. hr.offset:=hp^.pos;
  1817. emitpushreferenceaddr(hr);
  1818. emitcall('FPC_WIDESTR_DECR_REF');
  1819. end
  1820. else if hp^.temptype=tt_interfacecom then
  1821. begin
  1822. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1823. reset_reference(hr);
  1824. hr.base:=procinfo^.framepointer;
  1825. hr.offset:=hp^.pos;
  1826. emitpushreferenceaddr(hr);
  1827. emitcall('FPC_INTF_DECR_REF');
  1828. end;
  1829. hp:=hp^.next;
  1830. end;
  1831. end;
  1832. {$ifdef dummy}
  1833. var
  1834. ls : longint;
  1835. procedure largest_size(p : tnamedindexitem);
  1836. begin
  1837. if (tsym(p).typ=varsym) and
  1838. (tvarsym(p).getvaluesize>ls) then
  1839. ls:=tvarsym(p).getvaluesize;
  1840. end;
  1841. {$endif dummy}
  1842. procedure alignstack(alist : TAAsmoutput);
  1843. begin
  1844. {$ifdef dummy}
  1845. if (cs_optimize in aktglobalswitches) and
  1846. (aktoptprocessor in [classp5,classp6]) then
  1847. begin
  1848. ls:=0;
  1849. aktprocsym.definition.localst.foreach({$ifndef TP}@{$endif}largest_size);
  1850. if ls>=8 then
  1851. aList.insert(Taicpu.Op_const_reg(A_AND,S_L,-8,R_ESP));
  1852. end;
  1853. {$endif dummy}
  1854. end;
  1855. procedure genentrycode(alist : TAAsmoutput;make_global:boolean;
  1856. stackframe:longint;
  1857. var parasize:longint;var nostackframe:boolean;
  1858. inlined : boolean);
  1859. {
  1860. Generates the entry code for a procedure
  1861. }
  1862. var
  1863. hs : string;
  1864. {$ifdef GDB}
  1865. stab_function_name : tai_stab_function_name;
  1866. {$endif GDB}
  1867. hr : preference;
  1868. p : tsymtable;
  1869. r : treference;
  1870. oldlist,
  1871. oldexprasmlist : TAAsmoutput;
  1872. again : tasmlabel;
  1873. i : longint;
  1874. tempbuf,tempaddr : treference;
  1875. begin
  1876. oldexprasmlist:=exprasmlist;
  1877. exprasmlist:=alist;
  1878. if (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  1879. begin
  1880. emitinsertcall('FPC_INITIALIZEUNITS');
  1881. oldlist:=exprasmlist;
  1882. exprasmlist:=TAAsmoutput.Create;
  1883. p:=symtablestack;
  1884. while assigned(p) do
  1885. begin
  1886. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar);
  1887. p:=p.next;
  1888. end;
  1889. oldList.insertlist(exprasmlist);
  1890. exprasmlist.free;
  1891. exprasmlist:=oldlist;
  1892. end;
  1893. {$ifdef GDB}
  1894. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  1895. exprasmList.insert(Tai_force_line.Create);
  1896. {$endif GDB}
  1897. { a constructor needs a help procedure }
  1898. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  1899. begin
  1900. if is_class(procinfo^._class) then
  1901. begin
  1902. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1903. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1904. emitinsertcall('FPC_NEW_CLASS');
  1905. end
  1906. else if is_object(procinfo^._class) then
  1907. begin
  1908. exprasmList.insert(Taicpu.Op_cond_sym(A_Jcc,C_Z,S_NO,faillabel));
  1909. emitinsertcall('FPC_HELP_CONSTRUCTOR');
  1910. getexplicitregister32(R_EDI);
  1911. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  1912. end
  1913. else
  1914. Internalerror(200006161);
  1915. end;
  1916. { don't load ESI, does the caller }
  1917. { we must do it for local function }
  1918. { that can be called from a foreach_static }
  1919. { of another object than self !! PM }
  1920. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1921. (lexlevel>normal_function_level) then
  1922. maybe_loadself;
  1923. { When message method contains self as a parameter,
  1924. we must load it into ESI }
  1925. If (po_containsself in aktprocsym.definition.procoptions) then
  1926. begin
  1927. new(hr);
  1928. reset_reference(hr^);
  1929. hr^.offset:=procinfo^.selfpointer_offset;
  1930. hr^.base:=procinfo^.framepointer;
  1931. exprasmList.insert(Taicpu.Op_ref_reg(A_MOV,S_L,hr,R_ESI));
  1932. exprasmList.insert(Tairegalloc.Alloc(R_ESI));
  1933. end;
  1934. { should we save edi,esi,ebx like C ? }
  1935. if (po_savestdregs in aktprocsym.definition.procoptions) then
  1936. begin
  1937. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  1938. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBX));
  1939. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  1940. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI));
  1941. end;
  1942. { for the save all registers we can simply use a pusha,popa which
  1943. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1944. if (po_saveregisters in aktprocsym.definition.procoptions) then
  1945. begin
  1946. exprasmList.insert(Taicpu.Op_none(A_PUSHA,S_L));
  1947. end;
  1948. { omit stack frame ? }
  1949. if (not inlined) then
  1950. if (procinfo^.framepointer=stack_pointer) then
  1951. begin
  1952. CGMessage(cg_d_stackframe_omited);
  1953. nostackframe:=true;
  1954. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1955. parasize:=0
  1956. else
  1957. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-4;
  1958. if stackframe<>0 then
  1959. exprasmList.insert(Taicpu.op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  1960. end
  1961. else
  1962. begin
  1963. alignstack(alist);
  1964. if (aktprocsym.definition.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1965. parasize:=0
  1966. else
  1967. parasize:=aktprocsym.definition.parast.datasize+procinfo^.para_offset-8;
  1968. nostackframe:=false;
  1969. if stackframe<>0 then
  1970. begin
  1971. {$ifdef unused}
  1972. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  1973. begin
  1974. if (cs_check_stack in aktlocalswitches) and
  1975. not(target_info.target in [target_386_freebsd,
  1976. target_i386_linux,target_i386_win32]) then
  1977. begin
  1978. emitinsertcall('FPC_STACKCHECK');
  1979. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  1980. end;
  1981. if cs_profile in aktmoduleswitches then
  1982. genprofilecode;
  1983. { %edi is already saved when pocdecl is used
  1984. if ((target_info.target=target_linux) or (target_info.target=target_freebsd)) and
  1985. ((aktprocsym.definition.options and poexports)<>0) then
  1986. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EDI)); }
  1987. { ATTENTION:
  1988. never use ENTER in linux !!! (or freebsd MvdV)
  1989. the stack page fault does not support it PM }
  1990. exprasmList.insert(Taicpu.Op_const_const(A_ENTER,S_NO,stackframe,0)))
  1991. end
  1992. else
  1993. {$endif unused}
  1994. begin
  1995. {$ifndef NOTARGETWIN32}
  1996. { windows guards only a few pages for stack growing, }
  1997. { so we have to access every page first }
  1998. if (target_info.target=target_i386_win32) and
  1999. (stackframe>=winstackpagesize) then
  2000. begin
  2001. if stackframe div winstackpagesize<=5 then
  2002. begin
  2003. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe-4,R_ESP));
  2004. for i:=1 to stackframe div winstackpagesize do
  2005. begin
  2006. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  2007. exprasmList.concat(Taicpu.op_const_ref(A_MOV,S_L,0,hr));
  2008. end;
  2009. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2010. end
  2011. else
  2012. begin
  2013. getlabel(again);
  2014. getexplicitregister32(R_EDI);
  2015. exprasmList.concat(Taicpu.op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI));
  2016. emitlab(again);
  2017. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP));
  2018. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2019. exprasmList.concat(Taicpu.op_reg(A_DEC,S_L,R_EDI));
  2020. emitjmp(C_NZ,again);
  2021. ungetregister32(R_EDI);
  2022. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP));
  2023. end
  2024. end
  2025. else
  2026. {$endif NOTARGETWIN32}
  2027. exprasmList.insert(Taicpu.Op_const_reg(A_SUB,S_L,stackframe,R_ESP));
  2028. if (cs_check_stack in aktlocalswitches) and
  2029. not(target_info.target in [target_i386_freebsd,
  2030. target_i386_linux,target_i386_win32]) then
  2031. begin
  2032. emitinsertcall('FPC_STACKCHECK');
  2033. exprasmList.insert(Taicpu.Op_const(A_PUSH,S_L,stackframe));
  2034. end;
  2035. if cs_profile in aktmoduleswitches then
  2036. genprofilecode;
  2037. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2038. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2039. end;
  2040. end { endif stackframe <> 0 }
  2041. else
  2042. begin
  2043. if cs_profile in aktmoduleswitches then
  2044. genprofilecode;
  2045. exprasmList.insert(Taicpu.Op_reg_reg(A_MOV,S_L,R_ESP,R_EBP));
  2046. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_EBP));
  2047. end;
  2048. end;
  2049. if (po_interrupt in aktprocsym.definition.procoptions) then
  2050. generate_interrupt_stackframe_entry;
  2051. { initialize return value }
  2052. if (not is_void(procinfo^.returntype.def)) and
  2053. (procinfo^.returntype.def.needs_inittable) then
  2054. begin
  2055. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  2056. reset_reference(r);
  2057. r.offset:=procinfo^.return_offset;
  2058. r.base:=procinfo^.framepointer;
  2059. initialize(procinfo^.returntype.def,r,ret_in_param(procinfo^.returntype.def));
  2060. end;
  2061. { initialisize local data like ansistrings }
  2062. case aktprocsym.definition.proctypeoption of
  2063. potype_unitinit:
  2064. begin
  2065. { using current_module.globalsymtable is hopefully }
  2066. { more robust than symtablestack and symtablestack.next }
  2067. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2068. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data);
  2069. end;
  2070. { units have seperate code for initilization and finalization }
  2071. potype_unitfinalize: ;
  2072. else
  2073. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}initialize_data);
  2074. end;
  2075. { initialisizes temp. ansi/wide string data }
  2076. inittempvariables;
  2077. { generate copies of call by value parameters }
  2078. if not(po_assembler in aktprocsym.definition.procoptions) and
  2079. (([pocall_cdecl,pocall_cppdecl]*aktprocsym.definition.proccalloptions)=[]) then
  2080. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas);
  2081. if assigned( aktprocsym.definition.parast) then
  2082. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}init_paras);
  2083. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  2084. if not inlined and
  2085. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2086. { but it's useless in init/final code of units }
  2087. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2088. begin
  2089. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2090. exprasmList.concat(Taicpu.op_const_reg(A_SUB,S_L,36,R_ESP));
  2091. exprasmList.concat(Taicpu.op_reg_reg(A_MOV,S_L,R_ESP,R_EDI));
  2092. reset_reference(tempaddr);
  2093. tempaddr.base:=R_EDI;
  2094. emitpushreferenceaddr(tempaddr);
  2095. reset_reference(tempbuf);
  2096. tempbuf.base:=R_EDI;
  2097. tempbuf.offset:=12;
  2098. emitpushreferenceaddr(tempbuf);
  2099. { Type of stack-frame must be pushed}
  2100. exprasmList.concat(Taicpu.op_const(A_PUSH,S_L,1));
  2101. emitcall('FPC_PUSHEXCEPTADDR');
  2102. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2103. emitcall('FPC_SETJMP');
  2104. exprasmList.concat(Taicpu.op_reg(A_PUSH,S_L,R_EAX));
  2105. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2106. emitjmp(C_NE,aktexitlabel);
  2107. { probably we've to reload self here }
  2108. maybe_loadself;
  2109. end;
  2110. if not inlined then
  2111. begin
  2112. if (cs_profile in aktmoduleswitches) or
  2113. (aktprocsym.definition.owner.symtabletype=globalsymtable) or
  2114. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  2115. make_global:=true;
  2116. hs:=aktprocsym.definition.aliasnames.getfirst;
  2117. {$ifdef GDB}
  2118. if (cs_debuginfo in aktmoduleswitches) and target_info.use_function_relative_addresses then
  2119. stab_function_name := Tai_stab_function_name.Create(strpnew(hs));
  2120. {$EndIf GDB}
  2121. while hs<>'' do
  2122. begin
  2123. if make_global then
  2124. exprasmList.insert(Tai_symbol.Createname_global(hs,0))
  2125. else
  2126. exprasmList.insert(Tai_symbol.Createname(hs,0));
  2127. {$ifdef GDB}
  2128. if (cs_debuginfo in aktmoduleswitches) and
  2129. target_info.use_function_relative_addresses then
  2130. exprasmList.insert(Tai_stab_function_name.Create(strpnew(hs)));
  2131. {$endif GDB}
  2132. hs:=aktprocsym.definition.aliasnames.getfirst;
  2133. end;
  2134. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  2135. aktprocsym.is_global := True;
  2136. {$ifdef GDB}
  2137. if (cs_debuginfo in aktmoduleswitches) then
  2138. begin
  2139. if target_info.use_function_relative_addresses then
  2140. exprasmList.insert(stab_function_name);
  2141. exprasmList.insert(Tai_stabs.Create(aktprocsym.stabstring));
  2142. aktprocsym.isstabwritten:=true;
  2143. end;
  2144. {$endif GDB}
  2145. { Align, gprof uses 16 byte granularity }
  2146. if (cs_profile in aktmoduleswitches) then
  2147. exprasmList.insert(Tai_align.Create_op(16,$90))
  2148. else
  2149. exprasmList.insert(Tai_align.Create(aktalignment.procalign));
  2150. end;
  2151. if inlined then
  2152. load_regvars(exprasmlist,nil);
  2153. exprasmlist:=oldexprasmlist;
  2154. end;
  2155. procedure handle_return_value(inlined : boolean;var uses_eax,uses_edx : boolean);
  2156. var
  2157. hr : preference;
  2158. op : Tasmop;
  2159. s : Topsize;
  2160. begin
  2161. if not is_void(procinfo^.returntype.def) then
  2162. begin
  2163. {if ((procinfo^.flags and pi_operator)<>0) and
  2164. assigned(otsym) then
  2165. procinfo^.funcret_is_valid:=
  2166. procinfo^.funcret_is_valid or (otsym.refs>0);}
  2167. if (procinfo^.funcret_state<>vs_assigned) and not inlined { and
  2168. ((procinfo^.flags and pi_uses_asm)=0)} then
  2169. CGMessage(sym_w_function_result_not_set);
  2170. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2171. if (procinfo^.returntype.def.deftype in [orddef,enumdef]) then
  2172. begin
  2173. uses_eax:=true;
  2174. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2175. case procinfo^.returntype.def.size of
  2176. 8:
  2177. begin
  2178. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2179. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2180. exprasmList.concat(Tairegalloc.Alloc(R_EDX));
  2181. emit_ref_reg(A_MOV,S_L,hr,R_EDX);
  2182. uses_edx:=true;
  2183. end;
  2184. 4:
  2185. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2186. 2:
  2187. emit_ref_reg(A_MOV,S_W,hr,R_AX);
  2188. 1:
  2189. emit_ref_reg(A_MOV,S_B,hr,R_AL);
  2190. end;
  2191. end
  2192. else
  2193. if ret_in_acc(procinfo^.returntype.def) then
  2194. begin
  2195. uses_eax:=true;
  2196. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2197. emit_ref_reg(A_MOV,S_L,hr,R_EAX);
  2198. end
  2199. else
  2200. if (procinfo^.returntype.def.deftype=floatdef) then
  2201. begin
  2202. floatloadops(tfloatdef(procinfo^.returntype.def).typ,op,s);
  2203. exprasmList.concat(Taicpu.Op_ref(op,s,hr));
  2204. end
  2205. else
  2206. dispose(hr);
  2207. end
  2208. end;
  2209. procedure handle_fast_exit_return_value;
  2210. var
  2211. hr : preference;
  2212. op : Tasmop;
  2213. s : Topsize;
  2214. begin
  2215. if not is_void(procinfo^.returntype.def) then
  2216. begin
  2217. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset);
  2218. if (procinfo^.returntype.def.deftype in [orddef,enumdef]) then
  2219. begin
  2220. case procinfo^.returntype.def.size of
  2221. 8:
  2222. begin
  2223. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2224. hr:=new_reference(procinfo^.framepointer,procinfo^.return_offset+4);
  2225. emit_reg_ref(A_MOV,S_L,R_EDX,hr);
  2226. end;
  2227. 4:
  2228. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2229. 2:
  2230. emit_reg_ref(A_MOV,S_W,R_AX,hr);
  2231. 1:
  2232. emit_reg_ref(A_MOV,S_B,R_AL,hr);
  2233. end;
  2234. end
  2235. else
  2236. if ret_in_acc(procinfo^.returntype.def) then
  2237. begin
  2238. emit_reg_ref(A_MOV,S_L,R_EAX,hr);
  2239. end
  2240. else
  2241. if (procinfo^.returntype.def.deftype=floatdef) then
  2242. begin
  2243. floatstoreops(tfloatdef(procinfo^.returntype.def).typ,op,s);
  2244. exprasmlist.concat(taicpu.op_ref(op,s,hr));
  2245. end
  2246. else
  2247. dispose(hr);
  2248. end
  2249. end;
  2250. procedure genexitcode(alist : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  2251. var
  2252. {$ifdef GDB}
  2253. mangled_length : longint;
  2254. p : pchar;
  2255. st : string[2];
  2256. {$endif GDB}
  2257. stabsendlabel,nofinal,okexitlabel,
  2258. noreraiselabel,nodestroycall : tasmlabel;
  2259. hr : treference;
  2260. uses_eax,uses_edx,uses_esi : boolean;
  2261. oldexprasmlist : TAAsmoutput;
  2262. ai : taicpu;
  2263. pd : tprocdef;
  2264. begin
  2265. oldexprasmlist:=exprasmlist;
  2266. exprasmlist:=alist;
  2267. if aktexit2label.is_used and
  2268. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  2269. begin
  2270. exprasmlist.concat(taicpu.op_sym(A_JMP,S_NO,aktexitlabel));
  2271. exprasmlist.concat(tai_label.create(aktexit2label));
  2272. handle_fast_exit_return_value;
  2273. end;
  2274. if aktexitlabel.is_used then
  2275. exprasmList.concat(Tai_label.Create(aktexitlabel));
  2276. { call the destructor help procedure }
  2277. if (aktprocsym.definition.proctypeoption=potype_destructor) and
  2278. assigned(procinfo^._class) then
  2279. begin
  2280. if is_class(procinfo^._class) then
  2281. begin
  2282. emitinsertcall('FPC_DISPOSE_CLASS');
  2283. end
  2284. else if is_object(procinfo^._class) then
  2285. begin
  2286. emitinsertcall('FPC_HELP_DESTRUCTOR');
  2287. getexplicitregister32(R_EDI);
  2288. exprasmList.insert(Taicpu.Op_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI));
  2289. { must the object be finalized ? }
  2290. if procinfo^._class.needs_inittable then
  2291. begin
  2292. getlabel(nofinal);
  2293. exprasmList.insert(Tai_label.Create(nofinal));
  2294. emitinsertcall('FPC_FINALIZE');
  2295. ungetregister32(R_EDI);
  2296. exprasmList.insert(Taicpu.Op_reg(A_PUSH,S_L,R_ESI));
  2297. exprasmList.insert(Taicpu.Op_sym(A_PUSH,S_L,procinfo^._class.get_inittable_label));
  2298. ai:=Taicpu.Op_sym(A_Jcc,S_NO,nofinal);
  2299. ai.SetCondition(C_Z);
  2300. exprasmList.insert(ai);
  2301. reset_reference(hr);
  2302. hr.base:=R_EBP;
  2303. hr.offset:=8;
  2304. exprasmList.insert(Taicpu.Op_const_ref(A_CMP,S_L,0,newreference(hr)));
  2305. end;
  2306. end
  2307. else
  2308. begin
  2309. Internalerror(200006161);
  2310. end;
  2311. end;
  2312. { finalize temporary data }
  2313. finalizetempvariables;
  2314. { finalize local data like ansistrings}
  2315. case aktprocsym.definition.proctypeoption of
  2316. potype_unitfinalize:
  2317. begin
  2318. { using current_module.globalsymtable is hopefully }
  2319. { more robust than symtablestack and symtablestack.next }
  2320. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2321. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2322. end;
  2323. { units have seperate code for initialization and finalization }
  2324. potype_unitinit: ;
  2325. else
  2326. aktprocsym.definition.localst.foreach_static({$ifndef TP}@{$endif}finalize_data);
  2327. end;
  2328. { finalize paras data }
  2329. if assigned(aktprocsym.definition.parast) then
  2330. aktprocsym.definition.parast.foreach_static({$ifndef TP}@{$endif}final_paras);
  2331. { do we need to handle exceptions because of ansi/widestrings ? }
  2332. if not inlined and
  2333. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  2334. { but it's useless in init/final code of units }
  2335. not(aktprocsym.definition.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  2336. begin
  2337. { the exception helper routines modify all registers }
  2338. aktprocsym.definition.usedregisters:=$ff;
  2339. getlabel(noreraiselabel);
  2340. emitcall('FPC_POPADDRSTACK');
  2341. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2342. exprasmList.concat(Taicpu.op_reg(A_POP,S_L,R_EAX));
  2343. exprasmList.concat(Taicpu.op_reg_reg(A_TEST,S_L,R_EAX,R_EAX));
  2344. ungetregister32(R_EAX);
  2345. emitjmp(C_E,noreraiselabel);
  2346. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  2347. begin
  2348. if assigned(procinfo^._class) then
  2349. begin
  2350. pd:=procinfo^._class.searchdestructor;
  2351. if assigned(pd) then
  2352. begin
  2353. getlabel(nodestroycall);
  2354. emit_const_ref(A_CMP,S_L,0,new_reference(procinfo^.framepointer,
  2355. procinfo^.selfpointer_offset));
  2356. emitjmp(C_E,nodestroycall);
  2357. if is_class(procinfo^._class) then
  2358. begin
  2359. emit_const(A_PUSH,S_L,1);
  2360. emit_reg(A_PUSH,S_L,R_ESI);
  2361. end
  2362. else if is_object(procinfo^._class) then
  2363. begin
  2364. emit_reg(A_PUSH,S_L,R_ESI);
  2365. emit_sym(A_PUSH,S_L,newasmsymbol(procinfo^._class.vmt_mangledname));
  2366. end
  2367. else
  2368. begin
  2369. Internalerror(200006161);
  2370. end;
  2371. if (po_virtualmethod in pd.procoptions) then
  2372. begin
  2373. emit_ref_reg(A_MOV,S_L,new_reference(R_ESI,0),R_EDI);
  2374. emit_ref(A_CALL,S_NO,new_reference(R_EDI,procinfo^._class.vmtmethodoffset(pd.extnumber)));
  2375. end
  2376. else
  2377. emitcall(pd.mangledname);
  2378. { not necessary because the result is never assigned in the
  2379. case of an exception (FK)
  2380. emit_const_reg(A_MOV,S_L,0,R_ESI);
  2381. emit_const_ref(A_MOV,S_L,0,new_reference(procinfo^.framepointer,8));
  2382. }
  2383. emitlab(nodestroycall);
  2384. end;
  2385. end
  2386. end
  2387. else
  2388. { must be the return value finalized before reraising the exception? }
  2389. if (not is_void(procinfo^.returntype.def)) and
  2390. (procinfo^.returntype.def.needs_inittable) and
  2391. ((procinfo^.returntype.def.deftype<>objectdef) or
  2392. not is_class(procinfo^.returntype.def)) then
  2393. begin
  2394. reset_reference(hr);
  2395. hr.offset:=procinfo^.return_offset;
  2396. hr.base:=procinfo^.framepointer;
  2397. finalize(procinfo^.returntype.def,hr,ret_in_param(procinfo^.returntype.def));
  2398. end;
  2399. emitcall('FPC_RERAISE');
  2400. emitlab(noreraiselabel);
  2401. end;
  2402. { call __EXIT for main program }
  2403. if (not DLLsource) and (not inlined) and (aktprocsym.definition.proctypeoption=potype_proginit) then
  2404. begin
  2405. emitcall('FPC_DO_EXIT');
  2406. end;
  2407. { handle return value }
  2408. uses_eax:=false;
  2409. uses_edx:=false;
  2410. uses_esi:=false;
  2411. if not(po_assembler in aktprocsym.definition.procoptions) then
  2412. if (aktprocsym.definition.proctypeoption<>potype_constructor) then
  2413. handle_return_value(inlined,uses_eax,uses_edx)
  2414. else
  2415. begin
  2416. { successful constructor deletes the zero flag }
  2417. { and returns self in eax }
  2418. { eax must be set to zero if the allocation failed !!! }
  2419. getlabel(okexitlabel);
  2420. emitjmp(C_NONE,okexitlabel);
  2421. emitlab(faillabel);
  2422. if is_class(procinfo^._class) then
  2423. begin
  2424. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,8),R_ESI);
  2425. emitcall('FPC_HELP_FAIL_CLASS');
  2426. end
  2427. else if is_object(procinfo^._class) then
  2428. begin
  2429. emit_ref_reg(A_MOV,S_L,new_reference(procinfo^.framepointer,12),R_ESI);
  2430. getexplicitregister32(R_EDI);
  2431. emit_const_reg(A_MOV,S_L,procinfo^._class.vmt_offset,R_EDI);
  2432. emitcall('FPC_HELP_FAIL');
  2433. ungetregister32(R_EDI);
  2434. end
  2435. else
  2436. Internalerror(200006161);
  2437. emitlab(okexitlabel);
  2438. { for classes this is done after the call to }
  2439. { AfterConstruction }
  2440. if is_object(procinfo^._class) then
  2441. begin
  2442. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2443. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  2444. uses_eax:=true;
  2445. end;
  2446. emit_reg_reg(A_TEST,S_L,R_ESI,R_ESI);
  2447. uses_esi:=true;
  2448. end;
  2449. if aktexit2label.is_used and not aktexit2label.is_set then
  2450. emitlab(aktexit2label);
  2451. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2452. begin
  2453. getlabel(stabsendlabel);
  2454. emitlab(stabsendlabel);
  2455. end;
  2456. { gives problems for long mangled names }
  2457. {List.concat(Tai_symbol.Create(aktprocsym.definition.mangledname+'_end'));}
  2458. { should we restore edi ? }
  2459. { for all i386 gcc implementations }
  2460. if (po_savestdregs in aktprocsym.definition.procoptions) then
  2461. begin
  2462. if (aktprocsym.definition.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2463. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EBX));
  2464. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_ESI));
  2465. exprasmList.concat(Taicpu.Op_reg(A_POP,S_L,R_EDI));
  2466. { here we could reset R_EBX
  2467. but that is risky because it only works
  2468. if genexitcode is called after genentrycode
  2469. so lets skip this for the moment PM
  2470. aktprocsym.definition.usedregisters:=
  2471. aktprocsym.definition.usedregisters or not ($80 shr byte(R_EBX));
  2472. }
  2473. end;
  2474. { for the save all registers we can simply use a pusha,popa which
  2475. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  2476. if (po_saveregisters in aktprocsym.definition.procoptions) then
  2477. begin
  2478. if uses_esi then
  2479. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,4)));
  2480. if uses_edx then
  2481. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,20)));
  2482. if uses_eax then
  2483. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,28)));
  2484. exprasmList.concat(Taicpu.Op_none(A_POPA,S_L));
  2485. { We add a NOP because of the 386DX CPU bugs with POPAD }
  2486. exprasmlist.concat(taicpu.op_none(A_NOP,S_L));
  2487. end;
  2488. if not(nostackframe) then
  2489. begin
  2490. if not inlined then
  2491. exprasmList.concat(Taicpu.Op_none(A_LEAVE,S_NO));
  2492. end
  2493. else
  2494. begin
  2495. if (gettempsize<>0) and not inlined then
  2496. exprasmList.insert(Taicpu.op_const_reg(A_ADD,S_L,gettempsize,R_ESP));
  2497. end;
  2498. { parameters are limited to 65535 bytes because }
  2499. { ret allows only imm16 }
  2500. if (parasize>65535) and not(pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2501. CGMessage(cg_e_parasize_too_big);
  2502. { at last, the return is generated }
  2503. if not inlined then
  2504. if (po_interrupt in aktprocsym.definition.procoptions) then
  2505. begin
  2506. if uses_esi then
  2507. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_ESI,new_reference(R_ESP,16)));
  2508. if uses_edx then
  2509. begin
  2510. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2511. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EDX,new_reference(R_ESP,12)));
  2512. end;
  2513. if uses_eax then
  2514. begin
  2515. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  2516. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,0)));
  2517. end;
  2518. generate_interrupt_stackframe_exit;
  2519. end
  2520. else
  2521. begin
  2522. {Routines with the poclearstack flag set use only a ret.}
  2523. { also routines with parasize=0 }
  2524. if (pocall_clearstack in aktprocsym.definition.proccalloptions) then
  2525. begin
  2526. {$ifndef OLD_C_STACK}
  2527. { complex return values are removed from stack in C code PM }
  2528. if ret_in_param(aktprocsym.definition.rettype.def) then
  2529. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,4))
  2530. else
  2531. {$endif not OLD_C_STACK}
  2532. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2533. end
  2534. else if (parasize=0) then
  2535. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO))
  2536. else
  2537. exprasmList.concat(Taicpu.Op_const(A_RET,S_NO,parasize));
  2538. end;
  2539. if not inlined then
  2540. exprasmList.concat(Tai_symbol_end.Createname(aktprocsym.definition.mangledname));
  2541. {$ifdef GDB}
  2542. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2543. begin
  2544. aktprocsym.concatstabto(exprasmlist);
  2545. if assigned(procinfo^._class) then
  2546. if (not assigned(procinfo^.parent) or
  2547. not assigned(procinfo^.parent^._class)) then
  2548. begin
  2549. if (po_classmethod in aktprocsym.definition.procoptions) or
  2550. ((po_virtualmethod in aktprocsym.definition.procoptions) and
  2551. (potype_constructor=aktprocsym.definition.proctypeoption)) or
  2552. (po_staticmethod in aktprocsym.definition.procoptions) then
  2553. begin
  2554. exprasmList.concat(Tai_stabs.Create(strpnew(
  2555. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  2556. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2557. end
  2558. else
  2559. begin
  2560. if not(is_class(procinfo^._class)) then
  2561. st:='v'
  2562. else
  2563. st:='p';
  2564. exprasmList.concat(Tai_stabs.Create(strpnew(
  2565. '"$t:'+st+procinfo^._class.numberstring+'",'+
  2566. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  2567. end;
  2568. end
  2569. else
  2570. begin
  2571. if not is_class(procinfo^._class) then
  2572. st:='*'
  2573. else
  2574. st:='';
  2575. exprasmList.concat(Tai_stabs.Create(strpnew(
  2576. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  2577. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI]))));
  2578. end;
  2579. { define calling EBP as pseudo local var PM }
  2580. { this enables test if the function is a local one !! }
  2581. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  2582. exprasmList.concat(Tai_stabs.Create(strpnew(
  2583. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  2584. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  2585. if (not is_void(aktprocsym.definition.rettype.def)) then
  2586. begin
  2587. if ret_in_param(aktprocsym.definition.rettype.def) then
  2588. exprasmList.concat(Tai_stabs.Create(strpnew(
  2589. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2590. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2591. else
  2592. exprasmList.concat(Tai_stabs.Create(strpnew(
  2593. '"'+aktprocsym.name+':X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2594. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2595. if (m_result in aktmodeswitches) then
  2596. if ret_in_param(aktprocsym.definition.rettype.def) then
  2597. exprasmList.concat(Tai_stabs.Create(strpnew(
  2598. '"RESULT:X*'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2599. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  2600. else
  2601. exprasmList.concat(Tai_stabs.Create(strpnew(
  2602. '"RESULT:X'+tstoreddef(aktprocsym.definition.rettype.def).numberstring+'",'+
  2603. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  2604. end;
  2605. mangled_length:=length(aktprocsym.definition.mangledname);
  2606. getmem(p,2*mangled_length+50);
  2607. strpcopy(p,'192,0,0,');
  2608. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2609. if (target_info.use_function_relative_addresses) then
  2610. begin
  2611. strpcopy(strend(p),'-');
  2612. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2613. end;
  2614. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2615. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  2616. +aktprocsym.definition.mangledname))));
  2617. p[0]:='2';p[1]:='2';p[2]:='4';
  2618. strpcopy(strend(p),'_end');}
  2619. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  2620. if (target_info.use_function_relative_addresses) then
  2621. begin
  2622. strpcopy(strend(p),'-');
  2623. strpcopy(strend(p),aktprocsym.definition.mangledname);
  2624. end;
  2625. exprasmList.concatlist(withdebuglist);
  2626. exprasmList.concat(Tai_stabn.Create(strnew(p)));
  2627. { strpnew('224,0,0,'
  2628. +aktprocsym.definition.mangledname+'_end'))));}
  2629. freemem(p,2*mangled_length+50);
  2630. end;
  2631. {$endif GDB}
  2632. if inlined then
  2633. cleanup_regvars(exprasmlist);
  2634. exprasmlist:=oldexprasmlist;
  2635. end;
  2636. procedure genimplicitunitfinal(alist : TAAsmoutput);
  2637. begin
  2638. { using current_module.globalsymtable is hopefully }
  2639. { more robust than symtablestack and symtablestack.next }
  2640. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2641. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2642. exprasmList.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  2643. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  2644. {$ifdef GDB}
  2645. if (cs_debuginfo in aktmoduleswitches) and
  2646. target_info.use_function_relative_addresses then
  2647. exprasmList.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  2648. {$endif GDB}
  2649. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2650. aList.concatlist(exprasmlist);
  2651. end;
  2652. procedure genimplicitunitinit(alist : TAAsmoutput);
  2653. begin
  2654. { using current_module.globalsymtable is hopefully }
  2655. { more robust than symtablestack and symtablestack.next }
  2656. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2657. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data);
  2658. exprasmList.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  2659. exprasmList.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  2660. {$ifdef GDB}
  2661. if (cs_debuginfo in aktmoduleswitches) and
  2662. target_info.use_function_relative_addresses then
  2663. exprasmList.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  2664. {$endif GDB}
  2665. exprasmList.concat(Taicpu.Op_none(A_RET,S_NO));
  2666. aList.concatlist(exprasmlist);
  2667. end;
  2668. {$ifdef test_dest_loc}
  2669. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2670. begin
  2671. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2672. begin
  2673. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2674. set_location(p^.location,dest_loc);
  2675. in_dest_loc:=true;
  2676. end
  2677. else
  2678. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2679. begin
  2680. exprasmList.concat(Taicpu.Op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference)));
  2681. set_location(p^.location,dest_loc);
  2682. in_dest_loc:=true;
  2683. end
  2684. else
  2685. internalerror(20080);
  2686. end;
  2687. {$endif test_dest_loc}
  2688. end.
  2689. {
  2690. $Log$
  2691. Revision 1.26 2001-07-30 20:59:28 peter
  2692. * m68k updates from v10 merged
  2693. Revision 1.25 2001/07/01 20:16:18 peter
  2694. * alignmentinfo record added
  2695. * -Oa argument supports more alignment settings that can be specified
  2696. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  2697. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  2698. required alignment and the maximum usefull alignment. The final
  2699. alignment will be choosen per variable size dependent on these
  2700. settings
  2701. Revision 1.24 2001/05/27 14:30:55 florian
  2702. + some widestring stuff added
  2703. Revision 1.23 2001/04/21 13:33:16 peter
  2704. * move winstackpagesize const to cgai386 to remove uses t_win32
  2705. Revision 1.22 2001/04/21 12:05:32 peter
  2706. * add nop after popa (merged)
  2707. Revision 1.21 2001/04/18 22:02:00 peter
  2708. * registration of targets and assemblers
  2709. Revision 1.20 2001/04/13 01:22:17 peter
  2710. * symtable change to classes
  2711. * range check generation and errors fixed, make cycle DEBUG=1 works
  2712. * memory leaks fixed
  2713. Revision 1.19 2001/04/05 21:33:07 peter
  2714. * fast exit fix merged
  2715. Revision 1.18 2001/04/02 21:20:35 peter
  2716. * resulttype rewrite
  2717. Revision 1.17 2001/01/05 17:36:58 florian
  2718. * the info about exception frames is stored now on the stack
  2719. instead on the heap
  2720. Revision 1.16 2000/12/25 00:07:31 peter
  2721. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2722. tlinkedlist objects)
  2723. Revision 1.15 2000/12/05 11:44:32 jonas
  2724. + new integer regvar handling, should be much more efficient
  2725. Revision 1.14 2000/11/29 00:30:43 florian
  2726. * unused units removed from uses clause
  2727. * some changes for widestrings
  2728. Revision 1.13 2000/11/28 00:28:07 pierre
  2729. * stabs fixing
  2730. Revision 1.12 2000/11/22 15:12:06 jonas
  2731. * fixed inline-related problems (partially "merges")
  2732. Revision 1.11 2000/11/17 10:30:24 florian
  2733. * passing interfaces as parameters fixed
  2734. Revision 1.10 2000/11/07 23:40:48 florian
  2735. + AfterConstruction and BeforeDestruction impemented
  2736. Revision 1.9 2000/11/06 23:49:20 florian
  2737. * fixed init_paras call
  2738. Revision 1.8 2000/11/06 23:15:01 peter
  2739. * added copyvaluepara call again
  2740. Revision 1.7 2000/11/04 14:25:23 florian
  2741. + merged Attila's changes for interfaces, not tested yet
  2742. Revision 1.6 2000/10/31 22:02:55 peter
  2743. * symtable splitted, no real code changes
  2744. Revision 1.5 2000/10/24 22:23:04 peter
  2745. * emitcall -> emitinsertcall for profiling (merged)
  2746. Revision 1.4 2000/10/24 12:47:45 jonas
  2747. * allocate registers which hold function result
  2748. Revision 1.3 2000/10/24 08:54:25 michael
  2749. + Extra patch from peter
  2750. Revision 1.2 2000/10/24 07:20:03 pierre
  2751. * fix for bug 1193 (merged)
  2752. Revision 1.1 2000/10/15 09:47:42 peter
  2753. * moved to i386/
  2754. Revision 1.19 2000/10/14 10:14:46 peter
  2755. * moehrendorf oct 2000 rewrite
  2756. Revision 1.18 2000/10/10 14:55:28 jonas
  2757. * added missing regallocs for edi in emit_mov_ref_reg64 (merged)
  2758. Revision 1.17 2000/10/01 19:48:23 peter
  2759. * lot of compile updates for cg11
  2760. Revision 1.16 2000/09/30 16:08:45 peter
  2761. * more cg11 updates
  2762. Revision 1.15 2000/09/24 15:06:12 peter
  2763. * use defines.inc
  2764. Revision 1.14 2000/09/16 12:22:52 peter
  2765. * freebsd support merged
  2766. Revision 1.13 2000/08/27 16:11:49 peter
  2767. * moved some util functions from globals,cobjects to cutils
  2768. * splitted files into finput,fmodule
  2769. Revision 1.12 2000/08/24 19:07:54 peter
  2770. * don't initialize if localvarsym is set because that varsym will
  2771. already be initialized
  2772. * first initialize local data before copy of value para's (merged)
  2773. Revision 1.11 2000/08/19 20:09:33 peter
  2774. * check size after checking openarray in push_value_para (merged)
  2775. Revision 1.10 2000/08/16 13:06:06 florian
  2776. + support of 64 bit integer constants
  2777. Revision 1.9 2000/08/10 18:42:03 peter
  2778. * fixed for constants in emit_push_mem_size for go32v2 (merged)
  2779. Revision 1.8 2000/08/07 11:29:40 jonas
  2780. + emit_push_mem_size() which pushes a value in memory of a certain size
  2781. * pushsetelement() and pushvaluepara() use this new procedure, because
  2782. otherwise they could sometimes try to push data past the end of the
  2783. heap, causing a crash
  2784. (merged from fixes branch)
  2785. Revision 1.7 2000/08/03 13:17:25 jonas
  2786. + allow regvars to be used inside inlined procs, which required the
  2787. following changes:
  2788. + load regvars in genentrycode/free them in genexitcode (cgai386)
  2789. * moved all regvar related code to new regvars unit
  2790. + added pregvarinfo type to hcodegen
  2791. + added regvarinfo field to tprocinfo (symdef/symdefh)
  2792. * deallocate the regvars of the caller in secondprocinline before
  2793. inlining the called procedure and reallocate them afterwards
  2794. Revision 1.6 2000/08/02 08:05:04 jonas
  2795. * fixed web bug1087
  2796. * allocate R_ECX explicitely if it's used
  2797. (merged from fixes branch)
  2798. Revision 1.5 2000/07/27 09:25:05 jonas
  2799. * moved locflags2reg() procedure from cg386add to cgai386
  2800. + added locjump2reg() procedure to cgai386
  2801. * fixed internalerror(2002) when the result of a case expression has
  2802. LOC_JUMP
  2803. (all merged from fixes branch)
  2804. Revision 1.4 2000/07/21 15:14:02 jonas
  2805. + added is_addr field for labels, if they are only used for getting the address
  2806. (e.g. for io checks) and corresponding getaddrlabel() procedure
  2807. Revision 1.3 2000/07/13 12:08:25 michael
  2808. + patched to 1.1.0 with former 1.09patch from peter
  2809. Revision 1.2 2000/07/13 11:32:37 michael
  2810. + removed logs
  2811. }