cgai386.pas 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. unit cgai386;
  18. interface
  19. uses
  20. cobjects,tree,
  21. i386base,i386asm,
  22. {$ifdef dummy}
  23. end { to get correct syntax highlighting }
  24. {$endif dummy}
  25. aasm,symtable,win_targ;
  26. {$define TESTGETTEMP to store const that
  27. are written into temps for later release PM }
  28. function def_opsize(p1:pdef):topsize;
  29. function def2def_opsize(p1,p2:pdef):topsize;
  30. function def_getreg(p1:pdef):tregister;
  31. function makereg8(r:tregister):tregister;
  32. function makereg16(r:tregister):tregister;
  33. function makereg32(r:tregister):tregister;
  34. procedure emitlab(var l : pasmlabel);
  35. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  36. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  37. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  38. procedure emitcall(const routine:string);
  39. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference);
  40. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  41. procedure emit_push_loc(const t:tlocation);
  42. { pushes qword location to the stack }
  43. procedure emit_pushq_loc(const t : tlocation);
  44. procedure release_qword_loc(const t : tlocation);
  45. { releases the registers of a location }
  46. procedure release_loc(const t : tlocation);
  47. procedure emit_pushw_loc(const t:tlocation);
  48. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference);
  49. procedure emit_push_lea_loc(const t:tlocation);
  50. procedure emit_to_reference(var p:ptree);
  51. procedure emit_to_reg16(var hr:tregister);
  52. procedure emit_to_reg32(var hr:tregister);
  53. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  54. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  55. procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
  56. procedure loadansistring(p : ptree);
  57. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  58. procedure decrstringref(t : pdef;const ref : treference);
  59. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  60. procedure push_int(l : longint);
  61. procedure emit_push_mem(const ref : treference);
  62. procedure emitpushreferenceaddr(const ref : treference);
  63. procedure pushsetelement(p : ptree);
  64. procedure restore(p : ptree;isint64 : boolean);
  65. procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
  66. {$ifdef TEMPS_NOT_PUSH}
  67. { does the same as restore/maybe_push, but uses temp. space instead of pushing }
  68. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  69. procedure restorefromtemp(p : ptree;isint64 : boolean);
  70. {$endif TEMPS_NOT_PUSH}
  71. procedure floatload(t : tfloattype;const ref : treference);
  72. procedure floatstore(t : tfloattype;const ref : treference);
  73. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  74. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  75. procedure maybe_loadesi;
  76. procedure maketojumpbool(p : ptree);
  77. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;destreg:Tregister;delloc:boolean);
  78. procedure emitoverflowcheck(p:ptree);
  79. procedure emitrangecheck(p:ptree;todef:pdef);
  80. procedure concatcopy(source,dest : treference;size : longint;delsource : boolean;loadref:boolean);
  81. procedure firstcomplex(p : ptree);
  82. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  83. stackframe:longint;
  84. var parasize:longint;var nostackframe:boolean;
  85. inlined : boolean);
  86. procedure genexitcode(alist : paasmoutput;parasize:longint;
  87. nostackframe,inlined:boolean);
  88. {$ifdef test_dest_loc}
  89. const
  90. { used to avoid temporary assignments }
  91. dest_loc_known : boolean = false;
  92. in_dest_loc : boolean = false;
  93. dest_loc_tree : ptree = nil;
  94. var
  95. dest_loc : tlocation;
  96. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  97. {$endif test_dest_loc}
  98. implementation
  99. uses
  100. strings,globtype,systems,globals,verbose,files,types,pbase,
  101. tgeni386,temp_gen,hcodegen,ppu
  102. {$ifdef GDB}
  103. ,gdb
  104. {$endif}
  105. ;
  106. {*****************************************************************************
  107. Helpers
  108. *****************************************************************************}
  109. function def_opsize(p1:pdef):topsize;
  110. begin
  111. case p1^.size of
  112. 1 : def_opsize:=S_B;
  113. 2 : def_opsize:=S_W;
  114. 4 : def_opsize:=S_L;
  115. else
  116. internalerror(78);
  117. end;
  118. end;
  119. function def2def_opsize(p1,p2:pdef):topsize;
  120. var
  121. o1 : topsize;
  122. begin
  123. case p1^.size of
  124. 1 : o1:=S_B;
  125. 2 : o1:=S_W;
  126. 4 : o1:=S_L;
  127. { I don't know if we need it (FK) }
  128. 8 : o1:=S_L;
  129. else
  130. internalerror(78);
  131. end;
  132. if assigned(p2) then
  133. begin
  134. case p2^.size of
  135. 1 : o1:=S_B;
  136. 2 : begin
  137. if o1=S_B then
  138. o1:=S_BW
  139. else
  140. o1:=S_W;
  141. end;
  142. 4,8:
  143. begin
  144. case o1 of
  145. S_B : o1:=S_BL;
  146. S_W : o1:=S_WL;
  147. end;
  148. end;
  149. end;
  150. end;
  151. def2def_opsize:=o1;
  152. end;
  153. function def_getreg(p1:pdef):tregister;
  154. begin
  155. case p1^.size of
  156. 1 : def_getreg:=reg32toreg8(getregister32);
  157. 2 : def_getreg:=reg32toreg16(getregister32);
  158. 4 : def_getreg:=getregister32;
  159. else
  160. internalerror(78);
  161. end;
  162. end;
  163. function makereg8(r:tregister):tregister;
  164. begin
  165. case r of
  166. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  167. makereg8:=reg32toreg8(r);
  168. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  169. makereg8:=reg16toreg8(r);
  170. R_AL,R_BL,R_CL,R_DL :
  171. makereg8:=r;
  172. end;
  173. end;
  174. function makereg16(r:tregister):tregister;
  175. begin
  176. case r of
  177. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  178. makereg16:=reg32toreg16(r);
  179. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  180. makereg16:=r;
  181. R_AL,R_BL,R_CL,R_DL :
  182. makereg16:=reg8toreg16(r);
  183. end;
  184. end;
  185. function makereg32(r:tregister):tregister;
  186. begin
  187. case r of
  188. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  189. makereg32:=r;
  190. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  191. makereg32:=reg16toreg32(r);
  192. R_AL,R_BL,R_CL,R_DL :
  193. makereg32:=reg8toreg32(r);
  194. end;
  195. end;
  196. {*****************************************************************************
  197. Emit Assembler
  198. *****************************************************************************}
  199. procedure emitlab(var l : pasmlabel);
  200. begin
  201. if not l^.is_set then
  202. exprasmlist^.concat(new(pai_label,init(l)))
  203. else
  204. internalerror(7453984);
  205. end;
  206. {$ifdef nojmpfix}
  207. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  208. var
  209. ai : Pai386;
  210. begin
  211. if c=C_None then
  212. exprasmlist^.concat(new(pai386,op_sym(A_JMP,S_NO,l)))
  213. else
  214. begin
  215. ai:=new(pai386,op_sym(A_Jcc,S_NO,l));
  216. ai^.SetCondition(c);
  217. ai^.is_jmp:=true;
  218. exprasmlist^.concat(ai);
  219. end;
  220. end;
  221. {$else nojmpfix}
  222. procedure emitjmp(c : tasmcond;var l : pasmlabel);
  223. var
  224. ai : Pai386;
  225. begin
  226. if c=C_None then
  227. ai := new(pai386,op_sym(A_JMP,S_NO,l))
  228. else
  229. begin
  230. ai:=new(pai386,op_sym(A_Jcc,S_NO,l));
  231. ai^.SetCondition(c);
  232. end;
  233. ai^.is_jmp:=true;
  234. exprasmlist^.concat(ai);
  235. end;
  236. {$endif nojmpfix}
  237. procedure emit_flag2reg(flag:tresflags;hregister:tregister);
  238. var
  239. ai : pai386;
  240. hreg : tregister;
  241. begin
  242. hreg:=makereg8(hregister);
  243. ai:=new(pai386,op_reg(A_Setcc,S_B,hreg));
  244. ai^.SetCondition(flag_2_cond[flag]);
  245. exprasmlist^.concat(ai);
  246. if hreg<>hregister then
  247. begin
  248. if hregister in regset16bit then
  249. emit_to_reg16(hreg)
  250. else
  251. emit_to_reg32(hreg);
  252. end;
  253. end;
  254. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  255. begin
  256. if (reg1<>reg2) or (i<>A_MOV) then
  257. exprasmlist^.concat(new(pai386,op_reg_reg(i,s,reg1,reg2)));
  258. end;
  259. procedure emitcall(const routine:string);
  260. begin
  261. exprasmlist^.concat(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol(routine))));
  262. end;
  263. procedure emit_mov_loc_ref(const t:tlocation;const ref:treference);
  264. begin
  265. case t.loc of
  266. LOC_REGISTER,
  267. LOC_CREGISTER : begin
  268. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  269. t.register,newreference(ref))));
  270. ungetregister32(t.register); { the register is not needed anymore }
  271. end;
  272. LOC_MEM,
  273. LOC_REFERENCE : begin
  274. if t.reference.is_immediate then
  275. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,
  276. t.reference.offset,newreference(ref))))
  277. else
  278. begin
  279. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  280. newreference(t.reference),R_EDI)));
  281. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  282. R_EDI,newreference(ref))));
  283. { we can release the registers }
  284. { but only AFTER the MOV! Important for the optimizer!
  285. (JM)}
  286. del_reference(ref);
  287. end;
  288. ungetiftemp(t.reference);
  289. end;
  290. else
  291. internalerror(330);
  292. end;
  293. end;
  294. procedure emit_mov_loc_reg(const t:tlocation;reg:tregister);
  295. begin
  296. case t.loc of
  297. LOC_REGISTER,
  298. LOC_CREGISTER : begin
  299. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  300. t.register,reg)));
  301. ungetregister32(t.register); { the register is not needed anymore }
  302. end;
  303. LOC_MEM,
  304. LOC_REFERENCE : begin
  305. if t.reference.is_immediate then
  306. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,
  307. t.reference.offset,reg)))
  308. else
  309. begin
  310. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  311. newreference(t.reference),reg)));
  312. end;
  313. ungetiftemp(t.reference);
  314. end;
  315. else
  316. internalerror(330);
  317. end;
  318. end;
  319. procedure emit_mov_reg_loc(reg: TRegister; const t:tlocation);
  320. begin
  321. case t.loc of
  322. LOC_REGISTER,
  323. LOC_CREGISTER : begin
  324. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,RegSize(Reg),
  325. reg,t.register)));
  326. end;
  327. LOC_MEM,
  328. LOC_REFERENCE : begin
  329. if t.reference.is_immediate then
  330. internalerror(334)
  331. else
  332. begin
  333. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,RegSize(Reg),
  334. Reg,newreference(t.reference))));
  335. end;
  336. end;
  337. else
  338. internalerror(330);
  339. end;
  340. end;
  341. procedure emit_movq_reg_loc(reghigh,reglow: TRegister;t:tlocation);
  342. begin
  343. case t.loc of
  344. LOC_REGISTER,
  345. LOC_CREGISTER : begin
  346. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  347. reglow,t.registerlow)));
  348. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  349. reghigh,t.registerhigh)));
  350. end;
  351. LOC_MEM,
  352. LOC_REFERENCE : begin
  353. if t.reference.is_immediate then
  354. internalerror(334)
  355. else
  356. begin
  357. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  358. Reglow,newreference(t.reference))));
  359. inc(t.reference.offset,4);
  360. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  361. Reghigh,newreference(t.reference))));
  362. end;
  363. end;
  364. else
  365. internalerror(330);
  366. end;
  367. end;
  368. procedure emit_pushq_loc(const t : tlocation);
  369. var
  370. hr : preference;
  371. begin
  372. case t.loc of
  373. LOC_REGISTER,
  374. LOC_CREGISTER:
  375. begin
  376. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  377. t.registerhigh)));
  378. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,
  379. t.registerlow)));
  380. end;
  381. LOC_MEM,
  382. LOC_REFERENCE:
  383. begin
  384. hr:=newreference(t.reference);
  385. inc(hr^.offset,4);
  386. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  387. hr)));
  388. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  389. newreference(t.reference))));
  390. ungetiftemp(t.reference);
  391. end;
  392. else internalerror(331);
  393. end;
  394. end;
  395. procedure release_loc(const t : tlocation);
  396. begin
  397. case t.loc of
  398. LOC_REGISTER,
  399. LOC_CREGISTER:
  400. begin
  401. ungetregister32(t.register);
  402. end;
  403. LOC_MEM,
  404. LOC_REFERENCE:
  405. del_reference(t.reference);
  406. else internalerror(332);
  407. end;
  408. end;
  409. procedure release_qword_loc(const t : tlocation);
  410. begin
  411. case t.loc of
  412. LOC_REGISTER,
  413. LOC_CREGISTER:
  414. begin
  415. ungetregister32(t.registerhigh);
  416. ungetregister32(t.registerlow);
  417. end;
  418. LOC_MEM,
  419. LOC_REFERENCE:
  420. del_reference(t.reference);
  421. else internalerror(331);
  422. end;
  423. end;
  424. procedure emit_push_loc(const t:tlocation);
  425. begin
  426. case t.loc of
  427. LOC_REGISTER,
  428. LOC_CREGISTER : begin
  429. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,makereg32(t.register))));
  430. ungetregister(t.register); { the register is not needed anymore }
  431. end;
  432. LOC_MEM,
  433. LOC_REFERENCE : begin
  434. if t.reference.is_immediate then
  435. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,t.reference.offset)))
  436. else
  437. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(t.reference))));
  438. del_reference(t.reference);
  439. ungetiftemp(t.reference);
  440. end;
  441. else
  442. internalerror(330);
  443. end;
  444. end;
  445. procedure emit_pushw_loc(const t:tlocation);
  446. var
  447. opsize : topsize;
  448. begin
  449. case t.loc of
  450. LOC_REGISTER,
  451. LOC_CREGISTER : begin
  452. if target_os.stackalignment=4 then
  453. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,makereg32(t.register))))
  454. else
  455. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,makereg16(t.register))));
  456. ungetregister(t.register); { the register is not needed anymore }
  457. end;
  458. LOC_MEM,
  459. LOC_REFERENCE : begin
  460. if target_os.stackalignment=4 then
  461. opsize:=S_L
  462. else
  463. opsize:=S_W;
  464. if t.reference.is_immediate then
  465. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,t.reference.offset)))
  466. else
  467. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,newreference(t.reference))));
  468. del_reference(t.reference);
  469. ungetiftemp(t.reference);
  470. end;
  471. else
  472. internalerror(330);
  473. end;
  474. end;
  475. procedure emit_lea_loc_ref(const t:tlocation;const ref:treference);
  476. begin
  477. case t.loc of
  478. LOC_MEM,
  479. LOC_REFERENCE : begin
  480. if t.reference.is_immediate then
  481. internalerror(331)
  482. else
  483. begin
  484. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  485. newreference(t.reference),R_EDI)));
  486. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  487. R_EDI,newreference(ref))));
  488. end;
  489. ungetiftemp(t.reference);
  490. end;
  491. else
  492. internalerror(332);
  493. end;
  494. end;
  495. procedure emit_push_lea_loc(const t:tlocation);
  496. begin
  497. case t.loc of
  498. LOC_MEM,
  499. LOC_REFERENCE : begin
  500. if t.reference.is_immediate then
  501. internalerror(331)
  502. else
  503. begin
  504. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  505. newreference(t.reference),R_EDI)));
  506. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  507. end;
  508. { Wrong !!
  509. ungetiftemp(t.reference);}
  510. end;
  511. else
  512. internalerror(332);
  513. end;
  514. end;
  515. procedure emit_to_reference(var p:ptree);
  516. begin
  517. case p^.location.loc of
  518. LOC_FPU : begin
  519. reset_reference(p^.location.reference);
  520. gettempofsizereference(10,p^.location.reference);
  521. floatstore(pfloatdef(p^.resulttype)^.typ,p^.location.reference);
  522. p^.location.loc:=LOC_REFERENCE;
  523. end;
  524. LOC_MEM,
  525. LOC_REFERENCE : ;
  526. else
  527. internalerror(333);
  528. end;
  529. end;
  530. procedure emit_to_reg16(var hr:tregister);
  531. begin
  532. { ranges are a little bit bug sensitive ! }
  533. case hr of
  534. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP,R_EBP:
  535. begin
  536. hr:=reg32toreg16(hr);
  537. end;
  538. R_AL,R_BL,R_CL,R_DL:
  539. begin
  540. hr:=reg8toreg16(hr);
  541. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_W,$ff,hr)));
  542. end;
  543. R_AH,R_BH,R_CH,R_DH:
  544. begin
  545. hr:=reg8toreg16(hr);
  546. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_W,$ff00,hr)));
  547. end;
  548. end;
  549. end;
  550. procedure emit_to_reg32(var hr:tregister);
  551. begin
  552. { ranges are a little bit bug sensitive ! }
  553. case hr of
  554. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP,R_BP:
  555. begin
  556. hr:=reg16toreg32(hr);
  557. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ffff,hr)));
  558. end;
  559. R_AL,R_BL,R_CL,R_DL:
  560. begin
  561. hr:=reg8toreg32(hr);
  562. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ff,hr)));
  563. end;
  564. R_AH,R_BH,R_CH,R_DH:
  565. begin
  566. hr:=reg8toreg32(hr);
  567. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,$ff00,hr)));
  568. end;
  569. end;
  570. end;
  571. {*****************************************************************************
  572. Emit String Functions
  573. *****************************************************************************}
  574. procedure copyshortstring(const dref,sref : treference;len : byte;loadref:boolean);
  575. begin
  576. emitpushreferenceaddr(dref);
  577. if loadref then
  578. emit_push_mem(sref)
  579. else
  580. emitpushreferenceaddr(sref);
  581. push_int(len);
  582. emitcall('FPC_SHORTSTR_COPY');
  583. maybe_loadesi;
  584. end;
  585. procedure copylongstring(const dref,sref : treference;len : longint;loadref:boolean);
  586. begin
  587. emitpushreferenceaddr(dref);
  588. if loadref then
  589. emit_push_mem(sref)
  590. else
  591. emitpushreferenceaddr(sref);
  592. push_int(len);
  593. emitcall('FPC_LONGSTR_COPY');
  594. maybe_loadesi;
  595. end;
  596. procedure decrstringref(t : pdef;const ref : treference);
  597. var
  598. pushedregs : tpushed;
  599. begin
  600. pushusedregisters(pushedregs,$ff);
  601. emitpushreferenceaddr(ref);
  602. if is_ansistring(t) then
  603. begin
  604. exprasmlist^.concat(new(pai386,
  605. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ANSISTR_DECR_REF'))));
  606. end
  607. else if is_widestring(t) then
  608. begin
  609. exprasmlist^.concat(new(pai386,
  610. op_sym(A_CALL,S_NO,newasmsymbol('FPC_WIDESTR_DECR_REF'))));
  611. end
  612. else internalerror(1859);
  613. popusedregisters(pushedregs);
  614. end;
  615. procedure loadansistring(p : ptree);
  616. {
  617. copies an ansistring from p^.right to p^.left, we
  618. assume, that both sides are ansistring, firstassignement have
  619. to take care of that, an ansistring can't be a register variable
  620. }
  621. var
  622. pushed : tpushed;
  623. ungettemp : boolean;
  624. begin
  625. { before pushing any parameter, we have to save all used }
  626. { registers, but before that we have to release the }
  627. { registers of that node to save uneccessary pushed }
  628. { so be careful, if you think you can optimize that code (FK) }
  629. { nevertheless, this has to be changed, because otherwise the }
  630. { register is released before it's contents are pushed -> }
  631. { problems with the optimizer (JM) }
  632. del_reference(p^.left^.location.reference);
  633. ungettemp:=false;
  634. case p^.right^.location.loc of
  635. LOC_REGISTER,LOC_CREGISTER:
  636. begin
  637. {$IfNDef regallocfix}
  638. ungetregister32(p^.right^.location.register);
  639. pushusedregisters(pushed,$ff);
  640. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.right^.location.register)));
  641. {$Else regallocfix}
  642. pushusedregisters(pushed, $ff xor ($80 shr byte(p^.right^.location.register)));
  643. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.right^.location.register)));
  644. ungetregister32(p^.right^.location.register);
  645. {$EndIf regallocfix}
  646. end;
  647. LOC_REFERENCE,LOC_MEM:
  648. begin
  649. {$IfNDef regallocfix}
  650. del_reference(p^.right^.location.reference);
  651. pushusedregisters(pushed,$ff);
  652. emit_push_mem(p^.right^.location.reference);
  653. {$Else regallocfix}
  654. pushusedregisters(pushed,$ff
  655. xor ($80 shr byte(p^.right^.location.reference.base))
  656. xor ($80 shr byte(p^.right^.location.reference.index)));
  657. emit_push_mem(p^.right^.location.reference);
  658. del_reference(p^.right^.location.reference);
  659. {$EndIf regallocfix}
  660. ungettemp:=true;
  661. end;
  662. end;
  663. emitpushreferenceaddr(p^.left^.location.reference);
  664. del_reference(p^.left^.location.reference);
  665. emitcall('FPC_ANSISTR_ASSIGN');
  666. maybe_loadesi;
  667. popusedregisters(pushed);
  668. if ungettemp then
  669. ungetiftemp(p^.right^.location.reference);
  670. end;
  671. {*****************************************************************************
  672. Emit Push Functions
  673. *****************************************************************************}
  674. function maybe_push(needed : byte;p : ptree;isint64 : boolean) : boolean;
  675. var
  676. pushed : boolean;
  677. {hregister : tregister; }
  678. {$ifdef TEMPS_NOT_PUSH}
  679. href : treference;
  680. {$endif TEMPS_NOT_PUSH}
  681. begin
  682. if needed>usablereg32 then
  683. begin
  684. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  685. begin
  686. if isint64 then
  687. begin
  688. {$ifdef TEMPS_NOT_PUSH}
  689. gettempofsizereference(href,8);
  690. p^.temp_offset:=href.offset;
  691. href.offset:=href.offset+4;
  692. exprasmlist^.concat(new(pai386,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  693. href.offset:=href.offset-4;
  694. {$else TEMPS_NOT_PUSH}
  695. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.registerhigh)));
  696. {$endif TEMPS_NOT_PUSH}
  697. ungetregister32(p^.location.registerhigh);
  698. end
  699. {$ifdef TEMPS_NOT_PUSH}
  700. else
  701. begin
  702. gettempofsizereference(href,4);
  703. p^.temp_offset:=href.offset;
  704. end
  705. {$endif TEMPS_NOT_PUSH}
  706. ;
  707. pushed:=true;
  708. {$ifdef TEMPS_NOT_PUSH}
  709. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  710. {$else TEMPS_NOT_PUSH}
  711. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.register)));
  712. {$endif TEMPS_NOT_PUSH}
  713. ungetregister32(p^.location.register);
  714. end
  715. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  716. ((p^.location.reference.base<>R_NO) or
  717. (p^.location.reference.index<>R_NO)
  718. ) then
  719. begin
  720. del_reference(p^.location.reference);
  721. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  722. R_EDI)));
  723. {$ifdef TEMPS_NOT_PUSH}
  724. gettempofsizereference(href,4);
  725. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  726. p^.temp_offset:=href.offset;
  727. {$else TEMPS_NOT_PUSH}
  728. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  729. {$endif TEMPS_NOT_PUSH}
  730. pushed:=true;
  731. end
  732. else pushed:=false;
  733. end
  734. else pushed:=false;
  735. maybe_push:=pushed;
  736. end;
  737. {$ifdef TEMPS_NOT_PUSH}
  738. function maybe_savetotemp(needed : byte;p : ptree;isint64 : boolean) : boolean;
  739. var
  740. pushed : boolean;
  741. href : treference;
  742. begin
  743. if needed>usablereg32 then
  744. begin
  745. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  746. begin
  747. if isint64(p^.resulttype) then
  748. begin
  749. gettempofsizereference(href,8);
  750. p^.temp_offset:=href.offset;
  751. href.offset:=href.offset+4;
  752. exprasmlist^.concat(new(pai386,op_reg(A_MOV,S_L,p^.location.registerhigh,href)));
  753. href.offset:=href.offset-4;
  754. ungetregister32(p^.location.registerhigh);
  755. end
  756. else
  757. begin
  758. gettempofsizereference(href,4);
  759. p^.temp_offset:=href.offset;
  760. end;
  761. pushed:=true;
  762. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,href)));
  763. ungetregister32(p^.location.register);
  764. end
  765. else if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  766. ((p^.location.reference.base<>R_NO) or
  767. (p^.location.reference.index<>R_NO)
  768. ) then
  769. begin
  770. del_reference(p^.location.reference);
  771. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(p^.location.reference),
  772. R_EDI)));
  773. gettempofsizereference(href,4);
  774. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,href)));
  775. p^.temp_offset:=href.offset;
  776. pushed:=true;
  777. end
  778. else pushed:=false;
  779. end
  780. else pushed:=false;
  781. maybe_push:=pushed;
  782. end;
  783. {$endif TEMPS_NOT_PUSH}
  784. procedure push_int(l : longint);
  785. begin
  786. if (l = 0) and
  787. not(aktoptprocessor in [Class386, ClassP6]) and
  788. not(cs_littlesize in aktglobalswitches)
  789. Then
  790. begin
  791. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EDI,R_EDI)));
  792. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  793. end
  794. else
  795. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,l)));
  796. end;
  797. procedure emit_push_mem(const ref : treference);
  798. begin
  799. if ref.is_immediate then
  800. push_int(ref.offset)
  801. else
  802. begin
  803. if not(aktoptprocessor in [Class386, ClassP6]) and
  804. not(cs_littlesize in aktglobalswitches)
  805. then
  806. begin
  807. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(ref),R_EDI)));
  808. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  809. end
  810. else exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(ref))));
  811. end;
  812. end;
  813. procedure emitpushreferenceaddr(const ref : treference);
  814. var
  815. href : treference;
  816. begin
  817. { this will fail for references to other segments !!! }
  818. if ref.is_immediate then
  819. { is this right ? }
  820. begin
  821. { push_int(ref.offset)}
  822. gettempofsizereference(4,href);
  823. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,ref.offset,newreference(href))));
  824. emitpushreferenceaddr(href);
  825. del_reference(href);
  826. end
  827. else
  828. begin
  829. if ref.segment<>R_NO then
  830. CGMessage(cg_e_cant_use_far_pointer_there);
  831. if (ref.base=R_NO) and (ref.index=R_NO) then
  832. exprasmlist^.concat(new(pai386,op_sym_ofs(A_PUSH,S_L,ref.symbol,ref.offset)))
  833. else if (ref.base=R_NO) and (ref.index<>R_NO) and
  834. (ref.offset=0) and (ref.scalefactor=0) and (ref.symbol=nil) then
  835. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ref.index)))
  836. else if (ref.base<>R_NO) and (ref.index=R_NO) and
  837. (ref.offset=0) and (ref.symbol=nil) then
  838. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,ref.base)))
  839. else
  840. begin
  841. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(ref),R_EDI)));
  842. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  843. end;
  844. end;
  845. end;
  846. procedure pushsetelement(p : ptree);
  847. {
  848. copies p a set element on the stack
  849. }
  850. var
  851. hr,hr16,hr32 : tregister;
  852. begin
  853. { copy the element on the stack, slightly complicated }
  854. if p^.treetype=ordconstn then
  855. begin
  856. if target_os.stackalignment=4 then
  857. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,p^.value)))
  858. else
  859. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,p^.value)));
  860. end
  861. else
  862. begin
  863. case p^.location.loc of
  864. LOC_REGISTER,
  865. LOC_CREGISTER :
  866. begin
  867. hr:=p^.location.register;
  868. case hr of
  869. R_EAX,R_EBX,R_ECX,R_EDX,R_EDI,R_ESI,R_ESP :
  870. begin
  871. hr16:=reg32toreg16(hr);
  872. hr32:=hr;
  873. end;
  874. R_AX,R_BX,R_CX,R_DX,R_DI,R_SI,R_SP :
  875. begin
  876. hr16:=hr;
  877. hr32:=reg16toreg32(hr);
  878. end;
  879. R_AL,R_BL,R_CL,R_DL :
  880. begin
  881. hr16:=reg8toreg16(hr);
  882. hr32:=reg8toreg32(hr);
  883. end;
  884. end;
  885. if target_os.stackalignment=4 then
  886. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hr32)))
  887. else
  888. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,hr16)));
  889. ungetregister32(hr32);
  890. end;
  891. else
  892. begin
  893. if target_os.stackalignment=4 then
  894. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.location.reference))))
  895. else
  896. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,newreference(p^.location.reference))));
  897. del_reference(p^.location.reference);
  898. end;
  899. end;
  900. end;
  901. end;
  902. procedure restore(p : ptree;isint64 : boolean);
  903. var
  904. hregister : tregister;
  905. {$ifdef TEMPS_NOT_PUSH}
  906. href : treference;
  907. {$endif TEMPS_NOT_PUSH}
  908. begin
  909. hregister:=getregister32;
  910. {$ifdef TEMPS_NOT_PUSH}
  911. reset_reference(href);
  912. href.base:=procinfo.frame_pointer;
  913. href.offset:=p^.temp_offset;
  914. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,href,hregister)));
  915. {$else TEMPS_NOT_PUSH}
  916. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,hregister)));
  917. {$endif TEMPS_NOT_PUSH}
  918. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  919. begin
  920. p^.location.register:=hregister;
  921. if isint64 then
  922. begin
  923. p^.location.registerhigh:=getregister32;
  924. {$ifdef TEMPS_NOT_PUSH}
  925. href.offset:=p^.temp_offset+4;
  926. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,p^.location.registerhigh)));
  927. { set correctly for release ! }
  928. href.offset:=p^.temp_offset;
  929. {$else TEMPS_NOT_PUSH}
  930. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,p^.location.registerhigh)));
  931. {$endif TEMPS_NOT_PUSH}
  932. end;
  933. end
  934. else
  935. begin
  936. reset_reference(p^.location.reference);
  937. p^.location.reference.index:=hregister;
  938. set_location(p^.left^.location,p^.location);
  939. end;
  940. {$ifdef TEMPS_NOT_PUSH}
  941. ungetiftemp(href);
  942. {$endif TEMPS_NOT_PUSH}
  943. end;
  944. {$ifdef TEMPS_NOT_PUSH}
  945. procedure restorefromtemp(p : ptree;isint64 : boolean);
  946. var
  947. hregister : tregister;
  948. href : treference;
  949. begin
  950. hregister:=getregister32;
  951. reset_reference(href);
  952. href.base:=procinfo.frame_pointer;
  953. href.offset:=p^.temp_offset;
  954. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,href,hregister)));
  955. if (p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  956. begin
  957. p^.location.register:=hregister;
  958. if isint64 then
  959. begin
  960. p^.location.registerhigh:=getregister32;
  961. href.offset:=p^.temp_offset+4;
  962. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,p^.location.registerhigh)));
  963. { set correctly for release ! }
  964. href.offset:=p^.temp_offset;
  965. end;
  966. end
  967. else
  968. begin
  969. reset_reference(p^.location.reference);
  970. p^.location.reference.index:=hregister;
  971. set_location(p^.left^.location,p^.location);
  972. end;
  973. ungetiftemp(href);
  974. end;
  975. {$endif TEMPS_NOT_PUSH}
  976. procedure push_value_para(p:ptree;inlined:boolean;para_offset:longint;alignment : longint);
  977. var
  978. tempreference : treference;
  979. r : preference;
  980. opsize : topsize;
  981. op : tasmop;
  982. hreg : tregister;
  983. size : longint;
  984. hlabel : pasmlabel;
  985. begin
  986. case p^.location.loc of
  987. LOC_REGISTER,
  988. LOC_CREGISTER:
  989. begin
  990. case p^.location.register of
  991. R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
  992. R_EDI,R_ESP,R_EBP :
  993. begin
  994. inc(pushedparasize,4);
  995. if inlined then
  996. begin
  997. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  998. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  999. p^.location.register,r)));
  1000. end
  1001. else
  1002. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.location.register)));
  1003. ungetregister32(p^.location.register);
  1004. end;
  1005. R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
  1006. begin
  1007. if alignment=4 then
  1008. begin
  1009. opsize:=S_L;
  1010. hreg:=reg16toreg32(p^.location.register);
  1011. inc(pushedparasize,4);
  1012. end
  1013. else
  1014. begin
  1015. opsize:=S_W;
  1016. hreg:=p^.location.register;
  1017. inc(pushedparasize,2);
  1018. end;
  1019. if inlined then
  1020. begin
  1021. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1022. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1023. end
  1024. else
  1025. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1026. ungetregister32(reg16toreg32(p^.location.register));
  1027. end;
  1028. R_AL,R_BL,R_CL,R_DL:
  1029. begin
  1030. if alignment=4 then
  1031. begin
  1032. opsize:=S_L;
  1033. hreg:=reg8toreg32(p^.location.register);
  1034. inc(pushedparasize,4);
  1035. end
  1036. else
  1037. begin
  1038. opsize:=S_W;
  1039. hreg:=reg8toreg16(p^.location.register);
  1040. inc(pushedparasize,2);
  1041. end;
  1042. { we must push always 16 bit }
  1043. if inlined then
  1044. begin
  1045. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1046. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1047. end
  1048. else
  1049. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1050. ungetregister32(reg8toreg32(p^.location.register));
  1051. end;
  1052. end;
  1053. end;
  1054. LOC_FPU:
  1055. begin
  1056. size:=align(pfloatdef(p^.resulttype)^.size,alignment);
  1057. inc(pushedparasize,size);
  1058. if not inlined then
  1059. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  1060. {$ifdef GDB}
  1061. if (cs_debuginfo in aktmoduleswitches) and
  1062. (exprasmlist^.first=exprasmlist^.last) then
  1063. exprasmlist^.concat(new(pai_force_line,init));
  1064. {$endif GDB}
  1065. r:=new_reference(R_ESP,0);
  1066. floatstoreops(pfloatdef(p^.resulttype)^.typ,op,opsize);
  1067. { this is the easiest case for inlined !! }
  1068. if inlined then
  1069. begin
  1070. r^.base:=procinfo.framepointer;
  1071. r^.offset:=para_offset-pushedparasize;
  1072. end;
  1073. exprasmlist^.concat(new(pai386,op_ref(op,opsize,r)));
  1074. end;
  1075. LOC_REFERENCE,LOC_MEM:
  1076. begin
  1077. tempreference:=p^.location.reference;
  1078. del_reference(p^.location.reference);
  1079. case p^.resulttype^.deftype of
  1080. enumdef,
  1081. orddef :
  1082. begin
  1083. case p^.resulttype^.size of
  1084. 8 : begin
  1085. inc(pushedparasize,8);
  1086. if inlined then
  1087. begin
  1088. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1089. newreference(tempreference),R_EDI)));
  1090. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1091. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1092. inc(tempreference.offset,4);
  1093. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1094. newreference(tempreference),R_EDI)));
  1095. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize+4);
  1096. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1097. end
  1098. else
  1099. begin
  1100. inc(tempreference.offset,4);
  1101. emit_push_mem(tempreference);
  1102. dec(tempreference.offset,4);
  1103. emit_push_mem(tempreference);
  1104. end;
  1105. end;
  1106. 4 : begin
  1107. inc(pushedparasize,4);
  1108. if inlined then
  1109. begin
  1110. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1111. newreference(tempreference),R_EDI)));
  1112. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1113. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1114. end
  1115. else
  1116. emit_push_mem(tempreference);
  1117. end;
  1118. 1,2 : begin
  1119. if alignment=4 then
  1120. begin
  1121. opsize:=S_L;
  1122. hreg:=R_EDI;
  1123. inc(pushedparasize,4);
  1124. end
  1125. else
  1126. begin
  1127. opsize:=S_W;
  1128. hreg:=R_DI;
  1129. inc(pushedparasize,2);
  1130. end;
  1131. if inlined then
  1132. begin
  1133. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  1134. newreference(tempreference),hreg)));
  1135. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1136. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1137. end
  1138. else
  1139. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
  1140. newreference(tempreference))));
  1141. end;
  1142. else
  1143. internalerror(234231);
  1144. end;
  1145. end;
  1146. floatdef :
  1147. begin
  1148. case pfloatdef(p^.resulttype)^.typ of
  1149. f32bit,
  1150. s32real :
  1151. begin
  1152. inc(pushedparasize,4);
  1153. if inlined then
  1154. begin
  1155. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1156. newreference(tempreference),R_EDI)));
  1157. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1158. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1159. end
  1160. else
  1161. emit_push_mem(tempreference);
  1162. end;
  1163. s64real,
  1164. s64comp :
  1165. begin
  1166. inc(pushedparasize,4);
  1167. inc(tempreference.offset,4);
  1168. if inlined then
  1169. begin
  1170. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1171. newreference(tempreference),R_EDI)));
  1172. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1173. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1174. end
  1175. else
  1176. emit_push_mem(tempreference);
  1177. inc(pushedparasize,4);
  1178. dec(tempreference.offset,4);
  1179. if inlined then
  1180. begin
  1181. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1182. newreference(tempreference),R_EDI)));
  1183. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1184. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1185. end
  1186. else
  1187. emit_push_mem(tempreference);
  1188. end;
  1189. s80real :
  1190. begin
  1191. inc(pushedparasize,4);
  1192. if alignment=4 then
  1193. inc(tempreference.offset,8)
  1194. else
  1195. inc(tempreference.offset,6);
  1196. if inlined then
  1197. begin
  1198. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1199. newreference(tempreference),R_EDI)));
  1200. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1201. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1202. end
  1203. else
  1204. emit_push_mem(tempreference);
  1205. dec(tempreference.offset,4);
  1206. inc(pushedparasize,4);
  1207. if inlined then
  1208. begin
  1209. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1210. newreference(tempreference),R_EDI)));
  1211. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1212. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1213. end
  1214. else
  1215. emit_push_mem(tempreference);
  1216. if alignment=4 then
  1217. begin
  1218. opsize:=S_L;
  1219. hreg:=R_EDI;
  1220. inc(pushedparasize,4);
  1221. dec(tempreference.offset,4);
  1222. end
  1223. else
  1224. begin
  1225. opsize:=S_W;
  1226. hreg:=R_DI;
  1227. inc(pushedparasize,2);
  1228. dec(tempreference.offset,2);
  1229. end;
  1230. if inlined then
  1231. begin
  1232. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  1233. newreference(tempreference),hreg)));
  1234. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1235. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1236. end
  1237. else
  1238. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
  1239. newreference(tempreference))));
  1240. end;
  1241. end;
  1242. end;
  1243. pointerdef,
  1244. procvardef,
  1245. classrefdef:
  1246. begin
  1247. inc(pushedparasize,4);
  1248. if inlined then
  1249. begin
  1250. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1251. newreference(tempreference),R_EDI)));
  1252. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1253. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  1254. end
  1255. else
  1256. emit_push_mem(tempreference);
  1257. end;
  1258. arraydef,
  1259. recorddef,
  1260. stringdef,
  1261. setdef,
  1262. objectdef :
  1263. begin
  1264. { even some structured types are 32 bit }
  1265. if is_widestring(p^.resulttype) or
  1266. is_ansistring(p^.resulttype) or
  1267. is_smallset(p^.resulttype) or
  1268. ((p^.resulttype^.deftype in [recorddef,arraydef]) and (p^.resulttype^.size<=4)
  1269. and ((p^.resulttype^.deftype<>arraydef) or not
  1270. (parraydef(p^.resulttype)^.IsConstructor or
  1271. parraydef(p^.resulttype)^.isArrayOfConst or
  1272. is_open_array(p^.resulttype)))
  1273. ) or
  1274. ((p^.resulttype^.deftype=objectdef) and
  1275. pobjectdef(p^.resulttype)^.isclass) then
  1276. begin
  1277. inc(pushedparasize,4);
  1278. if inlined then
  1279. begin
  1280. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1281. concatcopy(tempreference,r^,4,false,false);
  1282. end
  1283. else
  1284. emit_push_mem(tempreference);
  1285. end
  1286. { call by value open array ? }
  1287. else
  1288. internalerror(8954);
  1289. end;
  1290. else
  1291. CGMessage(cg_e_illegal_expression);
  1292. end;
  1293. end;
  1294. LOC_JUMP:
  1295. begin
  1296. getlabel(hlabel);
  1297. if alignment=4 then
  1298. begin
  1299. opsize:=S_L;
  1300. inc(pushedparasize,4);
  1301. end
  1302. else
  1303. begin
  1304. opsize:=S_W;
  1305. inc(pushedparasize,2);
  1306. end;
  1307. emitlab(truelabel);
  1308. if inlined then
  1309. begin
  1310. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1311. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,1,r)));
  1312. end
  1313. else
  1314. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,1)));
  1315. emitjmp(C_None,hlabel);
  1316. emitlab(falselabel);
  1317. if inlined then
  1318. begin
  1319. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1320. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,0,r)));
  1321. end
  1322. else
  1323. exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,0)));
  1324. emitlab(hlabel);
  1325. end;
  1326. LOC_FLAGS:
  1327. begin
  1328. if not(R_EAX in unused) then
  1329. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));
  1330. emit_flag2reg(p^.location.resflags,R_AL);
  1331. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BW,R_AL,R_AX)));
  1332. if alignment=4 then
  1333. begin
  1334. opsize:=S_L;
  1335. hreg:=R_EAX;
  1336. inc(pushedparasize,4);
  1337. end
  1338. else
  1339. begin
  1340. opsize:=S_W;
  1341. hreg:=R_AX;
  1342. inc(pushedparasize,2);
  1343. end;
  1344. if inlined then
  1345. begin
  1346. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1347. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
  1348. end
  1349. else
  1350. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
  1351. if not(R_EAX in unused) then
  1352. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EDI,R_EAX)));
  1353. end;
  1354. {$ifdef SUPPORT_MMX}
  1355. LOC_MMXREGISTER,
  1356. LOC_CMMXREGISTER:
  1357. begin
  1358. inc(pushedparasize,8); { was missing !!! (PM) }
  1359. exprasmlist^.concat(new(pai386,op_const_reg(
  1360. A_SUB,S_L,8,R_ESP)));
  1361. {$ifdef GDB}
  1362. if (cs_debuginfo in aktmoduleswitches) and
  1363. (exprasmlist^.first=exprasmlist^.last) then
  1364. exprasmlist^.concat(new(pai_force_line,init));
  1365. {$endif GDB}
  1366. if inlined then
  1367. begin
  1368. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  1369. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  1370. p^.location.register,r)));
  1371. end
  1372. else
  1373. begin
  1374. r:=new_reference(R_ESP,0);
  1375. exprasmlist^.concat(new(pai386,op_reg_ref(
  1376. A_MOVQ,S_NO,p^.location.register,r)));
  1377. end;
  1378. end;
  1379. {$endif SUPPORT_MMX}
  1380. end;
  1381. end;
  1382. {*****************************************************************************
  1383. Emit Float Functions
  1384. *****************************************************************************}
  1385. procedure floatloadops(t : tfloattype;var op : tasmop;var s : topsize);
  1386. begin
  1387. case t of
  1388. s32real : begin
  1389. op:=A_FLD;
  1390. s:=S_FS;
  1391. end;
  1392. s64real : begin
  1393. op:=A_FLD;
  1394. { ???? }
  1395. s:=S_FL;
  1396. end;
  1397. s80real : begin
  1398. op:=A_FLD;
  1399. s:=S_FX;
  1400. end;
  1401. s64comp : begin
  1402. op:=A_FILD;
  1403. s:=S_IQ;
  1404. end;
  1405. else internalerror(17);
  1406. end;
  1407. end;
  1408. procedure floatload(t : tfloattype;const ref : treference);
  1409. var
  1410. op : tasmop;
  1411. s : topsize;
  1412. begin
  1413. floatloadops(t,op,s);
  1414. exprasmlist^.concat(new(pai386,op_ref(op,s,
  1415. newreference(ref))));
  1416. end;
  1417. procedure floatstoreops(t : tfloattype;var op : tasmop;var s : topsize);
  1418. begin
  1419. case t of
  1420. s32real : begin
  1421. op:=A_FSTP;
  1422. s:=S_FS;
  1423. end;
  1424. s64real : begin
  1425. op:=A_FSTP;
  1426. s:=S_FL;
  1427. end;
  1428. s80real : begin
  1429. op:=A_FSTP;
  1430. s:=S_FX;
  1431. end;
  1432. s64comp : begin
  1433. op:=A_FISTP;
  1434. s:=S_IQ;
  1435. end;
  1436. else
  1437. internalerror(17);
  1438. end;
  1439. end;
  1440. procedure floatstore(t : tfloattype;const ref : treference);
  1441. var
  1442. op : tasmop;
  1443. s : topsize;
  1444. begin
  1445. floatstoreops(t,op,s);
  1446. exprasmlist^.concat(new(pai386,op_ref(op,s,
  1447. newreference(ref))));
  1448. end;
  1449. {*****************************************************************************
  1450. Emit Functions
  1451. *****************************************************************************}
  1452. procedure maketojumpbool(p : ptree);
  1453. {
  1454. produces jumps to true respectively false labels using boolean expressions
  1455. }
  1456. var
  1457. opsize : topsize;
  1458. storepos : tfileposinfo;
  1459. begin
  1460. if p^.error then
  1461. exit;
  1462. storepos:=aktfilepos;
  1463. aktfilepos:=p^.fileinfo;
  1464. if is_boolean(p^.resulttype) then
  1465. begin
  1466. if is_constboolnode(p) then
  1467. begin
  1468. if p^.value<>0 then
  1469. emitjmp(C_None,truelabel)
  1470. else
  1471. emitjmp(C_None,falselabel);
  1472. end
  1473. else
  1474. begin
  1475. opsize:=def_opsize(p^.resulttype);
  1476. case p^.location.loc of
  1477. LOC_CREGISTER,LOC_REGISTER : begin
  1478. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,opsize,p^.location.register,
  1479. p^.location.register)));
  1480. ungetregister(p^.location.register);
  1481. emitjmp(C_NZ,truelabel);
  1482. emitjmp(C_None,falselabel);
  1483. end;
  1484. LOC_MEM,LOC_REFERENCE : begin
  1485. exprasmlist^.concat(new(pai386,op_const_ref(
  1486. A_CMP,opsize,0,newreference(p^.location.reference))));
  1487. del_reference(p^.location.reference);
  1488. emitjmp(C_NZ,truelabel);
  1489. emitjmp(C_None,falselabel);
  1490. end;
  1491. LOC_FLAGS : begin
  1492. emitjmp(flag_2_cond[p^.location.resflags],truelabel);
  1493. emitjmp(C_None,falselabel);
  1494. end;
  1495. end;
  1496. end;
  1497. end
  1498. else
  1499. CGMessage(type_e_mismatch);
  1500. aktfilepos:=storepos;
  1501. end;
  1502. { produces if necessary overflowcode }
  1503. procedure emitoverflowcheck(p:ptree);
  1504. var
  1505. hl : pasmlabel;
  1506. begin
  1507. if not(cs_check_overflow in aktlocalswitches) then
  1508. exit;
  1509. getlabel(hl);
  1510. if not ((p^.resulttype^.deftype=pointerdef) or
  1511. ((p^.resulttype^.deftype=orddef) and
  1512. (porddef(p^.resulttype)^.typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1513. bool8bit,bool16bit,bool32bit]))) then
  1514. emitjmp(C_NO,hl)
  1515. else
  1516. emitjmp(C_NB,hl);
  1517. emitcall('FPC_OVERFLOW');
  1518. emitlab(hl);
  1519. end;
  1520. { produces range check code, while one of the operands is a 64 bit
  1521. integer }
  1522. procedure emitrangecheck64(p : ptree;todef : pdef);
  1523. begin
  1524. internalerror(28699);
  1525. end;
  1526. { produces if necessary rangecheckcode }
  1527. procedure emitrangecheck(p:ptree;todef:pdef);
  1528. {
  1529. generate range checking code for the value at location t. The
  1530. type used is the checked against todefs ranges. fromdef (p.resulttype)
  1531. is the original type used at that location, when both defs are
  1532. equal the check is also insert (needed for succ,pref,inc,dec)
  1533. }
  1534. var
  1535. neglabel,
  1536. poslabel : pasmlabel;
  1537. href : treference;
  1538. rstr : string;
  1539. hreg : tregister;
  1540. opsize : topsize;
  1541. op : tasmop;
  1542. fromdef : pdef;
  1543. lto,hto,
  1544. lfrom,hfrom : longint;
  1545. doublebound,
  1546. is_reg,
  1547. popecx : boolean;
  1548. begin
  1549. { range checking on and range checkable value? }
  1550. if not(cs_check_range in aktlocalswitches) or
  1551. not(todef^.deftype in [orddef,enumdef,arraydef]) then
  1552. exit;
  1553. { only check when assigning to scalar, subranges are different,
  1554. when todef=fromdef then the check is always generated }
  1555. fromdef:=p^.resulttype;
  1556. if is_64bitint(fromdef) or is_64bitint(todef) then
  1557. begin
  1558. emitrangecheck64(p,todef);
  1559. exit;
  1560. end;
  1561. {we also need lto and hto when checking if we need to use doublebound!
  1562. (JM)}
  1563. getrange(todef,lto,hto);
  1564. if todef<>fromdef then
  1565. begin
  1566. getrange(p^.resulttype,lfrom,hfrom);
  1567. { first check for not being u32bit, then if the to is bigger than
  1568. from }
  1569. if (lto<hto) and (lfrom<hfrom) and
  1570. (lto<=lfrom) and (hto>=hfrom) then
  1571. exit;
  1572. end;
  1573. { generate the rangecheck code for the def where we are going to
  1574. store the result }
  1575. doublebound:=false;
  1576. case todef^.deftype of
  1577. orddef :
  1578. begin
  1579. porddef(todef)^.genrangecheck;
  1580. rstr:=porddef(todef)^.getrangecheckstring;
  1581. doublebound:=(porddef(todef)^.typ=u32bit) and (lto>hto);
  1582. end;
  1583. enumdef :
  1584. begin
  1585. penumdef(todef)^.genrangecheck;
  1586. rstr:=penumdef(todef)^.getrangecheckstring;
  1587. end;
  1588. arraydef :
  1589. begin
  1590. parraydef(todef)^.genrangecheck;
  1591. rstr:=parraydef(todef)^.getrangecheckstring;
  1592. end;
  1593. end;
  1594. { get op and opsize }
  1595. opsize:=def2def_opsize(fromdef,u32bitdef);
  1596. if opsize in [S_B,S_W,S_L] then
  1597. op:=A_MOV
  1598. else
  1599. if is_signed(fromdef) then
  1600. op:=A_MOVSX
  1601. else
  1602. op:=A_MOVZX;
  1603. is_reg:=(p^.location.loc in [LOC_REGISTER,LOC_CREGISTER]);
  1604. if is_reg then
  1605. hreg:=p^.location.register;
  1606. if not target_os.use_bound_instruction then
  1607. begin
  1608. { FPC_BOUNDCHECK needs to be called with
  1609. %ecx - value
  1610. %edi - pointer to the ranges }
  1611. popecx:=false;
  1612. if not(is_reg) or
  1613. (p^.location.register<>R_ECX) then
  1614. begin
  1615. if not(R_ECX in unused) then
  1616. begin
  1617. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  1618. popecx:=true;
  1619. end;
  1620. if is_reg then
  1621. exprasmlist^.concat(new(pai386,op_reg_reg(op,opsize,p^.location.register,R_ECX)))
  1622. else
  1623. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,newreference(p^.location.reference),R_ECX)));
  1624. end;
  1625. if doublebound then
  1626. begin
  1627. getlabel(neglabel);
  1628. getlabel(poslabel);
  1629. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,R_ECX,R_ECX)));
  1630. emitjmp(C_L,neglabel);
  1631. end;
  1632. { insert bound instruction only }
  1633. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),0,R_EDI)));
  1634. emitcall('FPC_BOUNDCHECK');
  1635. { u32bit needs 2 checks }
  1636. if doublebound then
  1637. begin
  1638. emitjmp(C_None,poslabel);
  1639. emitlab(neglabel);
  1640. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,newasmsymbol(rstr),8,R_EDI)));
  1641. emitcall('FPC_BOUNDCHECK');
  1642. emitlab(poslabel);
  1643. end;
  1644. if popecx then
  1645. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  1646. end
  1647. else
  1648. begin
  1649. reset_reference(href);
  1650. href.symbol:=newasmsymbol(rstr);
  1651. { load the value in a register }
  1652. if is_reg then
  1653. begin
  1654. { be sure that hreg is a 32 bit reg, if not load it in %edi }
  1655. if p^.location.register in [R_EAX..R_EDI] then
  1656. hreg:=p^.location.register
  1657. else
  1658. begin
  1659. exprasmlist^.concat(new(pai386,op_reg_reg(op,opsize,p^.location.register,R_EDI)));
  1660. hreg:=R_EDI;
  1661. end;
  1662. end
  1663. else
  1664. begin
  1665. exprasmlist^.concat(new(pai386,op_ref_reg(op,opsize,newreference(p^.location.reference),R_EDI)));
  1666. hreg:=R_EDI;
  1667. end;
  1668. if doublebound then
  1669. begin
  1670. getlabel(neglabel);
  1671. getlabel(poslabel);
  1672. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hreg,hreg)));
  1673. emitjmp(C_L,neglabel);
  1674. end;
  1675. { insert bound instruction only }
  1676. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  1677. { u32bit needs 2 checks }
  1678. if doublebound then
  1679. begin
  1680. href.offset:=8;
  1681. emitjmp(C_None,poslabel);
  1682. emitlab(neglabel);
  1683. exprasmlist^.concat(new(pai386,op_reg_ref(A_BOUND,S_L,hreg,newreference(href))));
  1684. emitlab(poslabel);
  1685. end;
  1686. end;
  1687. end;
  1688. procedure concatcopy(source,dest : treference;size : longint;delsource,loadref : boolean);
  1689. const
  1690. isizes : array[0..3] of topsize=(S_L,S_B,S_W,S_B);
  1691. ishr : array[0..3] of byte=(2,0,1,0);
  1692. var
  1693. ecxpushed : boolean;
  1694. helpsize : longint;
  1695. i : byte;
  1696. reg8,reg32 : tregister;
  1697. swap : boolean;
  1698. procedure maybepushecx;
  1699. begin
  1700. if not(R_ECX in unused) then
  1701. begin
  1702. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  1703. ecxpushed:=true;
  1704. end;
  1705. end;
  1706. begin
  1707. {$IfNDef regallocfix}
  1708. If delsource then
  1709. del_reference(source);
  1710. {$EndIf regallocfix}
  1711. if (not loadref) and
  1712. ((size<=8) or
  1713. (not(cs_littlesize in aktglobalswitches ) and (size<=12))) then
  1714. begin
  1715. helpsize:=size shr 2;
  1716. for i:=1 to helpsize do
  1717. begin
  1718. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(source),R_EDI)));
  1719. {$ifdef regallocfix}
  1720. If (size = 4) and delsource then
  1721. del_reference(source);
  1722. {$endif regallocfix}
  1723. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(dest))));
  1724. inc(source.offset,4);
  1725. inc(dest.offset,4);
  1726. dec(size,4);
  1727. end;
  1728. if size>1 then
  1729. begin
  1730. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(source),R_DI)));
  1731. {$ifdef regallocfix}
  1732. If (size = 2) and delsource then
  1733. del_reference(source);
  1734. {$endif regallocfix}
  1735. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,R_DI,newreference(dest))));
  1736. inc(source.offset,2);
  1737. inc(dest.offset,2);
  1738. dec(size,2);
  1739. end;
  1740. if size>0 then
  1741. begin
  1742. { and now look for an 8 bit register }
  1743. swap:=false;
  1744. if R_EAX in unused then reg8:=R_AL
  1745. else if R_EBX in unused then reg8:=R_BL
  1746. else if R_ECX in unused then reg8:=R_CL
  1747. else if R_EDX in unused then reg8:=R_DL
  1748. else
  1749. begin
  1750. swap:=true;
  1751. { we need only to check 3 registers, because }
  1752. { one is always not index or base }
  1753. if (dest.base<>R_EAX) and (dest.index<>R_EAX) then
  1754. begin
  1755. reg8:=R_AL;
  1756. reg32:=R_EAX;
  1757. end
  1758. else if (dest.base<>R_EBX) and (dest.index<>R_EBX) then
  1759. begin
  1760. reg8:=R_BL;
  1761. reg32:=R_EBX;
  1762. end
  1763. else if (dest.base<>R_ECX) and (dest.index<>R_ECX) then
  1764. begin
  1765. reg8:=R_CL;
  1766. reg32:=R_ECX;
  1767. end;
  1768. end;
  1769. if swap then
  1770. { was earlier XCHG, of course nonsense }
  1771. emit_reg_reg(A_MOV,S_L,reg32,R_EDI);
  1772. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,newreference(source),reg8)));
  1773. {$ifdef regallocfix}
  1774. If delsource then
  1775. del_reference(source);
  1776. {$endif regallocfix}
  1777. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_B,reg8,newreference(dest))));
  1778. if swap then
  1779. emit_reg_reg(A_MOV,S_L,R_EDI,reg32);
  1780. end;
  1781. end
  1782. else
  1783. begin
  1784. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(dest),R_EDI)));
  1785. {$ifdef regallocfix}
  1786. {is this ok?? (JM)}
  1787. del_reference(dest);
  1788. {$endif regallocfix}
  1789. if loadref then
  1790. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(source),R_ESI)))
  1791. else
  1792. begin
  1793. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(source),R_ESI)));
  1794. {$ifdef regallocfix}
  1795. if delsource then
  1796. del_reference(source);
  1797. {$endif regallocfix}
  1798. end;
  1799. exprasmlist^.concat(new(pai386,op_none(A_CLD,S_NO)));
  1800. ecxpushed:=false;
  1801. if cs_littlesize in aktglobalswitches then
  1802. begin
  1803. maybepushecx;
  1804. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,size,R_ECX)));
  1805. exprasmlist^.concat(new(pai386,op_none(A_REP,S_NO)));
  1806. exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  1807. end
  1808. else
  1809. begin
  1810. helpsize:=size shr 2;
  1811. size:=size and 3;
  1812. if helpsize>1 then
  1813. begin
  1814. maybepushecx;
  1815. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,helpsize,R_ECX)));
  1816. exprasmlist^.concat(new(pai386,op_none(A_REP,S_NO)));
  1817. end;
  1818. if helpsize>0 then
  1819. exprasmlist^.concat(new(pai386,op_none(A_MOVSD,S_NO)));
  1820. if size>1 then
  1821. begin
  1822. dec(size,2);
  1823. exprasmlist^.concat(new(pai386,op_none(A_MOVSW,S_NO)));
  1824. end;
  1825. if size=1 then
  1826. exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  1827. end;
  1828. if ecxpushed then
  1829. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  1830. { loading SELF-reference again }
  1831. maybe_loadesi;
  1832. end;
  1833. if delsource then
  1834. ungetiftemp(source);
  1835. end;
  1836. procedure emitloadord2reg(const location:Tlocation;orddef:Porddef;
  1837. destreg:Tregister;delloc:boolean);
  1838. {A lot smaller and less bug sensitive than the original unfolded loads.}
  1839. var tai:Pai386;
  1840. r:Preference;
  1841. begin
  1842. case location.loc of
  1843. LOC_REGISTER,LOC_CREGISTER:
  1844. begin
  1845. case orddef^.typ of
  1846. u8bit:
  1847. tai:=new(pai386,op_reg_reg(A_MOVZX,S_BL,location.register,destreg));
  1848. s8bit:
  1849. tai:=new(pai386,op_reg_reg(A_MOVSX,S_BL,location.register,destreg));
  1850. u16bit:
  1851. tai:=new(pai386,op_reg_reg(A_MOVZX,S_WL,location.register,destreg));
  1852. s16bit:
  1853. tai:=new(pai386,op_reg_reg(A_MOVSX,S_WL,location.register,destreg));
  1854. u32bit:
  1855. tai:=new(pai386,op_reg_reg(A_MOV,S_L,location.register,destreg));
  1856. s32bit:
  1857. tai:=new(pai386,op_reg_reg(A_MOV,S_L,location.register,destreg));
  1858. end;
  1859. if delloc then
  1860. ungetregister(location.register);
  1861. end;
  1862. LOC_MEM,
  1863. LOC_REFERENCE:
  1864. begin
  1865. if location.reference.is_immediate then
  1866. tai:=new(pai386,op_const_reg(A_MOV,S_L,location.reference.offset,destreg))
  1867. else
  1868. begin
  1869. r:=newreference(location.reference);
  1870. case orddef^.typ of
  1871. u8bit:
  1872. tai:=new(pai386,op_ref_reg(A_MOVZX,S_BL,r,destreg));
  1873. s8bit:
  1874. tai:=new(pai386,op_ref_reg(A_MOVSX,S_BL,r,destreg));
  1875. u16bit:
  1876. tai:=new(pai386,op_ref_reg(A_MOVZX,S_WL,r,destreg));
  1877. s16bit:
  1878. tai:=new(pai386,op_ref_reg(A_MOVSX,S_WL,r,destreg));
  1879. u32bit:
  1880. tai:=new(pai386,op_ref_reg(A_MOV,S_L,r,destreg));
  1881. s32bit:
  1882. tai:=new(pai386,op_ref_reg(A_MOV,S_L,r,destreg));
  1883. end;
  1884. end;
  1885. if delloc then
  1886. del_reference(location.reference);
  1887. end
  1888. else
  1889. internalerror(6);
  1890. end;
  1891. exprasmlist^.concat(tai);
  1892. end;
  1893. { if necessary ESI is reloaded after a call}
  1894. procedure maybe_loadesi;
  1895. var
  1896. hp : preference;
  1897. p : pprocinfo;
  1898. i : longint;
  1899. begin
  1900. if assigned(procinfo._class) then
  1901. begin
  1902. if lexlevel>normal_function_level then
  1903. begin
  1904. new(hp);
  1905. reset_reference(hp^);
  1906. hp^.offset:=procinfo.framepointer_offset;
  1907. hp^.base:=procinfo.framepointer;
  1908. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1909. p:=procinfo.parent;
  1910. for i:=3 to lexlevel-1 do
  1911. begin
  1912. new(hp);
  1913. reset_reference(hp^);
  1914. hp^.offset:=p^.framepointer_offset;
  1915. hp^.base:=R_ESI;
  1916. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1917. p:=p^.parent;
  1918. end;
  1919. new(hp);
  1920. reset_reference(hp^);
  1921. hp^.offset:=p^.ESI_offset;
  1922. hp^.base:=R_ESI;
  1923. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1924. end
  1925. else
  1926. begin
  1927. new(hp);
  1928. reset_reference(hp^);
  1929. hp^.offset:=procinfo.ESI_offset;
  1930. hp^.base:=procinfo.framepointer;
  1931. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,R_ESI)));
  1932. end;
  1933. end;
  1934. end;
  1935. procedure firstcomplex(p : ptree);
  1936. var
  1937. hp : ptree;
  1938. begin
  1939. { always calculate boolean AND and OR from left to right }
  1940. if (p^.treetype in [orn,andn]) and
  1941. (p^.left^.resulttype^.deftype=orddef) and
  1942. (porddef(p^.left^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]) then
  1943. p^.swaped:=false
  1944. else
  1945. if (p^.left^.registers32<p^.right^.registers32) and
  1946. { the following check is appropriate, because all }
  1947. { 4 registers are rarely used and it is thereby }
  1948. { achieved that the extra code is being dropped }
  1949. { by exchanging not commutative operators }
  1950. (p^.right^.registers32<=4) then
  1951. begin
  1952. hp:=p^.left;
  1953. p^.left:=p^.right;
  1954. p^.right:=hp;
  1955. p^.swaped:=true;
  1956. end
  1957. else
  1958. p^.swaped:=false;
  1959. end;
  1960. {*****************************************************************************
  1961. Entry/Exit Code Functions
  1962. *****************************************************************************}
  1963. procedure genprofilecode;
  1964. var
  1965. pl : pasmlabel;
  1966. begin
  1967. if (aktprocsym^.definition^.options and poassembler)<>0 then
  1968. exit;
  1969. case target_info.target of
  1970. target_i386_linux:
  1971. begin
  1972. getlabel(pl);
  1973. emitcall('mcount');
  1974. exprasmlist^.insert(new(pai386,op_sym_ofs_reg(A_MOV,S_L,pl,0,R_EDX)));
  1975. exprasmlist^.insert(new(pai_section,init(sec_code)));
  1976. exprasmlist^.insert(new(pai_const,init_32bit(0)));
  1977. exprasmlist^.insert(new(pai_label,init(pl)));
  1978. exprasmlist^.insert(new(pai_align,init(4)));
  1979. exprasmlist^.insert(new(pai_section,init(sec_data)));
  1980. end;
  1981. target_i386_go32v2:
  1982. begin
  1983. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('MCOUNT'))));
  1984. end;
  1985. end;
  1986. end;
  1987. procedure generate_interrupt_stackframe_entry;
  1988. begin
  1989. { save the registers of an interrupt procedure }
  1990. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  1991. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  1992. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  1993. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  1994. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  1995. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  1996. { .... also the segment registers }
  1997. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_DS)));
  1998. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_ES)));
  1999. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_FS)));
  2000. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_W,R_GS)));
  2001. end;
  2002. procedure generate_interrupt_stackframe_exit;
  2003. begin
  2004. { restore the registers of an interrupt procedure }
  2005. { this was all with entrycode instead of exitcode !!}
  2006. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  2007. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  2008. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  2009. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  2010. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  2011. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  2012. { .... also the segment registers }
  2013. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_DS)));
  2014. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_ES)));
  2015. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_FS)));
  2016. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_GS)));
  2017. { this restores the flags }
  2018. procinfo.aktexitcode^.concat(new(pai386,op_none(A_IRET,S_NO)));
  2019. end;
  2020. { generates the code for threadvar initialisation }
  2021. procedure initialize_threadvar(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2022. var
  2023. hr : treference;
  2024. begin
  2025. if (psym(p)^.typ=varsym) and
  2026. ((pvarsym(p)^.var_options and vo_is_thread_var)<>0) then
  2027. begin
  2028. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pvarsym(p)^.getsize)));
  2029. reset_reference(hr);
  2030. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2031. emitpushreferenceaddr(hr);
  2032. exprasmlist^.concat(new(pai386,
  2033. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INIT_THREADVAR'))));
  2034. end;
  2035. end;
  2036. { initilizes data of type t }
  2037. { if is_already_ref is true then the routines assumes }
  2038. { that r points to the data to initialize }
  2039. procedure initialize(t : pdef;const ref : treference;is_already_ref : boolean);
  2040. var
  2041. hr : treference;
  2042. begin
  2043. if is_ansistring(t) or
  2044. is_widestring(t) then
  2045. begin
  2046. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,
  2047. newreference(ref))));
  2048. end
  2049. else
  2050. begin
  2051. reset_reference(hr);
  2052. hr.symbol:=t^.get_inittable_label;
  2053. emitpushreferenceaddr(hr);
  2054. if is_already_ref then
  2055. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  2056. newreference(ref))))
  2057. else
  2058. emitpushreferenceaddr(ref);
  2059. exprasmlist^.concat(new(pai386,
  2060. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INITIALIZE'))));
  2061. end;
  2062. end;
  2063. { finalizes data of type t }
  2064. { if is_already_ref is true then the routines assumes }
  2065. { that r points to the data to finalizes }
  2066. procedure finalize(t : pdef;const ref : treference;is_already_ref : boolean);
  2067. var
  2068. r : treference;
  2069. begin
  2070. if is_ansistring(t) or
  2071. is_widestring(t) then
  2072. begin
  2073. decrstringref(t,ref);
  2074. end
  2075. else
  2076. begin
  2077. reset_reference(r);
  2078. r.symbol:=t^.get_inittable_label;
  2079. emitpushreferenceaddr(r);
  2080. if is_already_ref then
  2081. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,
  2082. newreference(ref))))
  2083. else
  2084. emitpushreferenceaddr(ref);
  2085. emitcall('FPC_FINALIZE');
  2086. end;
  2087. end;
  2088. { generates the code for initialisation of local data }
  2089. procedure initialize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2090. var
  2091. hr : treference;
  2092. begin
  2093. if (psym(p)^.typ=varsym) and
  2094. assigned(pvarsym(p)^.definition) and
  2095. pvarsym(p)^.definition^.needs_inittable and
  2096. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2097. pobjectdef(pvarsym(p)^.definition)^.isclass) then
  2098. begin
  2099. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2100. reset_reference(hr);
  2101. if psym(p)^.owner^.symtabletype=localsymtable then
  2102. begin
  2103. hr.base:=procinfo.framepointer;
  2104. hr.offset:=-pvarsym(p)^.address;
  2105. end
  2106. else
  2107. begin
  2108. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2109. end;
  2110. initialize(pvarsym(p)^.definition,hr,false);
  2111. end;
  2112. end;
  2113. { generates the code for incrementing the reference count of parameters }
  2114. procedure incr_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2115. var
  2116. hr : treference;
  2117. begin
  2118. if (psym(p)^.typ=varsym) and
  2119. pvarsym(p)^.definition^.needs_inittable and
  2120. ((pvarsym(p)^.varspez=vs_value) {or
  2121. (pvarsym(p)^.varspez=vs_const) and
  2122. not(dont_copy_const_param(pvarsym(p)^.definition))}) and
  2123. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2124. pobjectdef(pvarsym(p)^.definition)^.isclass) then
  2125. begin
  2126. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2127. reset_reference(hr);
  2128. hr.symbol:=pvarsym(p)^.definition^.get_inittable_label;
  2129. emitpushreferenceaddr(hr);
  2130. reset_reference(hr);
  2131. hr.base:=procinfo.framepointer;
  2132. hr.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2133. emitpushreferenceaddr(hr);
  2134. reset_reference(hr);
  2135. exprasmlist^.concat(new(pai386,
  2136. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ADDREF'))));
  2137. end;
  2138. end;
  2139. { generates the code for finalisation of local data }
  2140. procedure finalize_data(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  2141. var
  2142. hr : treference;
  2143. begin
  2144. if (psym(p)^.typ=varsym) and
  2145. assigned(pvarsym(p)^.definition) and
  2146. pvarsym(p)^.definition^.needs_inittable and
  2147. not((pvarsym(p)^.definition^.deftype=objectdef) and
  2148. pobjectdef(pvarsym(p)^.definition)^.isclass) then
  2149. begin
  2150. { not all kind of parameters need to be finalized }
  2151. if (psym(p)^.owner^.symtabletype=parasymtable) and
  2152. ((pvarsym(p)^.varspez=vs_var) or
  2153. (pvarsym(p)^.varspez=vs_const) { and
  2154. (dont_copy_const_param(pvarsym(p)^.definition)) } ) then
  2155. exit;
  2156. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2157. reset_reference(hr);
  2158. case psym(p)^.owner^.symtabletype of
  2159. localsymtable:
  2160. begin
  2161. hr.base:=procinfo.framepointer;
  2162. hr.offset:=-pvarsym(p)^.address;
  2163. end;
  2164. parasymtable:
  2165. begin
  2166. hr.base:=procinfo.framepointer;
  2167. hr.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2168. end;
  2169. else
  2170. hr.symbol:=newasmsymbol(pvarsym(p)^.mangledname);
  2171. end;
  2172. finalize(pvarsym(p)^.definition,hr,false);
  2173. end;
  2174. end;
  2175. { generates the code to make local copies of the value parameters }
  2176. procedure copyvalueparas(p : pnamedindexobject);{$ifndef fpc}far;{$endif}
  2177. var
  2178. href1,href2 : treference;
  2179. r : preference;
  2180. len : longint;
  2181. opsize : topsize;
  2182. again,ok : pasmlabel;
  2183. begin
  2184. if (psym(p)^.typ=varsym) and
  2185. (pvarsym(p)^.varspez=vs_value) and
  2186. (push_addr_param(pvarsym(p)^.definition)) then
  2187. begin
  2188. if is_open_array(pvarsym(p)^.definition) or
  2189. is_array_of_const(pvarsym(p)^.definition) then
  2190. begin
  2191. { get stack space }
  2192. new(r);
  2193. reset_reference(r^);
  2194. r^.base:=procinfo.framepointer;
  2195. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2196. exprasmlist^.concat(new(pai386,
  2197. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2198. exprasmlist^.concat(new(pai386,
  2199. op_reg(A_INC,S_L,R_EDI)));
  2200. exprasmlist^.concat(new(pai386,
  2201. op_const_reg(A_IMUL,S_L,
  2202. parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
  2203. { windows guards only a few pages for stack growing, }
  2204. { so we have to access every page first }
  2205. if target_os.id=os_i386_win32 then
  2206. begin
  2207. getlabel(again);
  2208. getlabel(ok);
  2209. emitlab(again);
  2210. exprasmlist^.concat(new(pai386,
  2211. op_const_reg(A_CMP,S_L,winstackpagesize,R_EDI)));
  2212. emitjmp(C_C,ok);
  2213. exprasmlist^.concat(new(pai386,
  2214. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2215. exprasmlist^.concat(new(pai386,
  2216. op_reg(A_PUSH,S_L,R_EAX)));
  2217. exprasmlist^.concat(new(pai386,
  2218. op_const_reg(A_SUB,S_L,winstackpagesize,R_EDI)));
  2219. emitjmp(C_None,again);
  2220. emitlab(ok);
  2221. exprasmlist^.concat(new(pai386,
  2222. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2223. { now reload EDI }
  2224. new(r);
  2225. reset_reference(r^);
  2226. r^.base:=procinfo.framepointer;
  2227. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2228. exprasmlist^.concat(new(pai386,
  2229. op_ref_reg(A_MOV,S_L,r,R_EDI)));
  2230. exprasmlist^.concat(new(pai386,
  2231. op_reg(A_INC,S_L,R_EDI)));
  2232. exprasmlist^.concat(new(pai386,
  2233. op_const_reg(A_IMUL,S_L,
  2234. parraydef(pvarsym(p)^.definition)^.definition^.size,R_EDI)));
  2235. end
  2236. else
  2237. begin
  2238. exprasmlist^.concat(new(pai386,
  2239. op_reg_reg(A_SUB,S_L,R_EDI,R_ESP)));
  2240. { load destination }
  2241. exprasmlist^.concat(new(pai386,
  2242. op_reg_reg(A_MOV,S_L,R_ESP,R_EDI)));
  2243. end;
  2244. { don't destroy the registers! }
  2245. exprasmlist^.concat(new(pai386,
  2246. op_reg(A_PUSH,S_L,R_ECX)));
  2247. exprasmlist^.concat(new(pai386,
  2248. op_reg(A_PUSH,S_L,R_ESI)));
  2249. { load count }
  2250. new(r);
  2251. reset_reference(r^);
  2252. r^.base:=procinfo.framepointer;
  2253. r^.offset:=pvarsym(p)^.address+4+procinfo.call_offset;
  2254. exprasmlist^.concat(new(pai386,
  2255. op_ref_reg(A_MOV,S_L,r,R_ECX)));
  2256. { load source }
  2257. new(r);
  2258. reset_reference(r^);
  2259. r^.base:=procinfo.framepointer;
  2260. r^.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2261. exprasmlist^.concat(new(pai386,
  2262. op_ref_reg(A_MOV,S_L,r,R_ESI)));
  2263. { scheduled .... }
  2264. exprasmlist^.concat(new(pai386,
  2265. op_reg(A_INC,S_L,R_ECX)));
  2266. { calculate size }
  2267. len:=parraydef(pvarsym(p)^.definition)^.definition^.size;
  2268. opsize:=S_B;
  2269. if (len and 3)=0 then
  2270. begin
  2271. opsize:=S_L;
  2272. len:=len shr 2;
  2273. end
  2274. else
  2275. if (len and 1)=0 then
  2276. begin
  2277. opsize:=S_W;
  2278. len:=len shr 1;
  2279. end;
  2280. exprasmlist^.concat(new(pai386,
  2281. op_const_reg(A_IMUL,S_L,len,R_ECX)));
  2282. exprasmlist^.concat(new(pai386,
  2283. op_none(A_REP,S_NO)));
  2284. case opsize of
  2285. S_B : exprasmlist^.concat(new(pai386,op_none(A_MOVSB,S_NO)));
  2286. S_W : exprasmlist^.concat(new(pai386,op_none(A_MOVSW,S_NO)));
  2287. S_L : exprasmlist^.concat(new(pai386,op_none(A_MOVSD,S_NO)));
  2288. end;
  2289. exprasmlist^.concat(new(pai386,
  2290. op_reg(A_POP,S_L,R_ESI)));
  2291. exprasmlist^.concat(new(pai386,
  2292. op_reg(A_POP,S_L,R_ECX)));
  2293. { patch the new address }
  2294. new(r);
  2295. reset_reference(r^);
  2296. r^.base:=procinfo.framepointer;
  2297. r^.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2298. exprasmlist^.concat(new(pai386,
  2299. op_reg_ref(A_MOV,S_L,R_ESP,r)));
  2300. end
  2301. else
  2302. if is_shortstring(pvarsym(p)^.definition) then
  2303. begin
  2304. reset_reference(href1);
  2305. href1.base:=procinfo.framepointer;
  2306. href1.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2307. reset_reference(href2);
  2308. href2.base:=procinfo.framepointer;
  2309. href2.offset:=-pvarsym(p)^.localvarsym^.address;
  2310. copyshortstring(href2,href1,pstringdef(pvarsym(p)^.definition)^.len,true);
  2311. end
  2312. else
  2313. begin
  2314. reset_reference(href1);
  2315. href1.base:=procinfo.framepointer;
  2316. href1.offset:=pvarsym(p)^.address+procinfo.call_offset;
  2317. reset_reference(href2);
  2318. href2.base:=procinfo.framepointer;
  2319. href2.offset:=-pvarsym(p)^.localvarsym^.address;
  2320. concatcopy(href1,href2,pvarsym(p)^.definition^.size,true,true);
  2321. end;
  2322. end;
  2323. end;
  2324. procedure inittempansistrings;
  2325. var
  2326. hp : ptemprecord;
  2327. r : preference;
  2328. begin
  2329. hp:=templist;
  2330. while assigned(hp) do
  2331. begin
  2332. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2333. begin
  2334. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2335. new(r);
  2336. reset_reference(r^);
  2337. r^.base:=procinfo.framepointer;
  2338. r^.offset:=hp^.pos;
  2339. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,0,r)));
  2340. end;
  2341. hp:=hp^.next;
  2342. end;
  2343. end;
  2344. procedure finalizetempansistrings;
  2345. var
  2346. hp : ptemprecord;
  2347. hr : treference;
  2348. begin
  2349. hp:=templist;
  2350. while assigned(hp) do
  2351. begin
  2352. if hp^.temptype in [tt_ansistring,tt_freeansistring] then
  2353. begin
  2354. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2355. reset_reference(hr);
  2356. hr.base:=procinfo.framepointer;
  2357. hr.offset:=hp^.pos;
  2358. emitpushreferenceaddr(hr);
  2359. exprasmlist^.concat(new(pai386,
  2360. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ANSISTR_DECR_REF'))));
  2361. end;
  2362. hp:=hp^.next;
  2363. end;
  2364. end;
  2365. procedure genentrycode(alist : paasmoutput;const proc_names:Tstringcontainer;make_global:boolean;
  2366. stackframe:longint;
  2367. var parasize:longint;var nostackframe:boolean;
  2368. inlined : boolean);
  2369. {
  2370. Generates the entry code for a procedure
  2371. }
  2372. var
  2373. hs : string;
  2374. {$ifdef GDB}
  2375. stab_function_name : Pai_stab_function_name;
  2376. {$endif GDB}
  2377. hr : preference;
  2378. p : psymtable;
  2379. r : treference;
  2380. oldlist,
  2381. oldexprasmlist : paasmoutput;
  2382. again : pasmlabel;
  2383. i : longint;
  2384. begin
  2385. oldexprasmlist:=exprasmlist;
  2386. exprasmlist:=alist;
  2387. if (not inlined) and ((aktprocsym^.definition^.options and poproginit)<>0) then
  2388. begin
  2389. exprasmlist^.insert(new(pai386,
  2390. op_sym(A_CALL,S_NO,newasmsymbol('FPC_INITIALIZEUNITS'))));
  2391. if target_info.target=target_I386_WIN32 then
  2392. begin
  2393. new(hr);
  2394. reset_reference(hr^);
  2395. hr^.symbol:=newasmsymbol('U_SYSWIN32_ISCONSOLE');
  2396. if apptype=at_cui then
  2397. exprasmlist^.insert(new(pai386,op_const_ref(A_MOV,S_B,
  2398. 1,hr)))
  2399. else
  2400. exprasmlist^.insert(new(pai386,op_const_ref(A_MOV,S_B,
  2401. 0,hr)));
  2402. end;
  2403. oldlist:=exprasmlist;
  2404. exprasmlist:=new(paasmoutput,init);
  2405. p:=symtablestack;
  2406. while assigned(p) do
  2407. begin
  2408. p^.foreach({$ifndef TP}@{$endif}initialize_threadvar);
  2409. p:=p^.next;
  2410. end;
  2411. oldlist^.insertlist(exprasmlist);
  2412. dispose(exprasmlist,done);
  2413. exprasmlist:=oldlist;
  2414. end;
  2415. { a constructor needs a help procedure }
  2416. if (aktprocsym^.definition^.options and poconstructor)<>0 then
  2417. begin
  2418. if procinfo._class^.isclass then
  2419. begin
  2420. exprasmlist^.insert(new(pai386,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  2421. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_NEW_CLASS'))));
  2422. end
  2423. else
  2424. begin
  2425. exprasmlist^.insert(new(pai386,op_cond_sym(A_Jcc,C_Z,S_NO,quickexitlabel)));
  2426. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_HELP_CONSTRUCTOR'))));
  2427. exprasmlist^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  2428. end;
  2429. end;
  2430. { don't load ESI, does the caller }
  2431. { When message method contains self as a parameter,
  2432. we must load it into ESI }
  2433. If ((aktprocsym^.definition^.options and pocontainsself)<>0) then
  2434. begin
  2435. new(hr);
  2436. reset_reference(hr^);
  2437. hr^.offset:=procinfo.ESI_offset;
  2438. hr^.base:=procinfo.framepointer;
  2439. exprasmlist^.insert(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_ESI)));
  2440. end;
  2441. { should we save edi ? }
  2442. if ((aktprocsym^.definition^.options and posavestdregs)<>0) then
  2443. begin
  2444. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2445. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  2446. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  2447. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  2448. end;
  2449. { omit stack frame ? }
  2450. if not inlined then
  2451. if procinfo.framepointer=stack_pointer then
  2452. begin
  2453. CGMessage(cg_d_stackframe_omited);
  2454. nostackframe:=true;
  2455. if (aktprocsym^.definition^.options and (pounitinit or poproginit or pounitfinalize)<>0) then
  2456. parasize:=0
  2457. else
  2458. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-4;
  2459. end
  2460. else
  2461. begin
  2462. if (aktprocsym^.definition^.options and (pounitinit or poproginit or pounitfinalize)<>0) then
  2463. parasize:=0
  2464. else
  2465. parasize:=aktprocsym^.definition^.parast^.datasize+procinfo.call_offset-8;
  2466. nostackframe:=false;
  2467. if stackframe<>0 then
  2468. begin
  2469. {$ifdef unused}
  2470. if (cs_littlesize in aktglobalswitches) and (stackframe<=65535) then
  2471. begin
  2472. if (cs_check_stack in aktlocalswitches) and
  2473. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  2474. begin
  2475. exprasmlist^.insert(new(pai386,
  2476. op_sym(A_CALL,S_NO,newasmsymbol('FPC_STACKCHECK'))));
  2477. exprasmlist^.insert(new(pai386,op_const(A_PUSH,S_L,stackframe)));
  2478. end;
  2479. if cs_profile in aktmoduleswitches then
  2480. genprofilecode;
  2481. { %edi is already saved when pocdecl is used
  2482. if (target_info.target=target_linux) and
  2483. ((aktprocsym^.definition^.options and poexports)<>0) then
  2484. exprasmlist^.insert(new(Pai386,op_reg(A_PUSH,S_L,R_EDI))); }
  2485. exprasmlist^.insert(new(pai386,op_const_const(A_ENTER,S_NO,stackframe,0)))
  2486. end
  2487. else
  2488. {$endif unused}
  2489. begin
  2490. { windows guards only a few pages for stack growing, }
  2491. { so we have to access every page first }
  2492. if (target_os.id=os_i386_win32) and
  2493. (stackframe>=winstackpagesize) then
  2494. begin
  2495. if stackframe div winstackpagesize<=5 then
  2496. begin
  2497. exprasmlist^.insert(new(pai386,op_const_reg(A_SUB,S_L,stackframe-4,R_ESP)));
  2498. for i:=1 to stackframe div winstackpagesize do
  2499. begin
  2500. hr:=new_reference(R_ESP,stackframe-i*winstackpagesize);
  2501. exprasmlist^.concat(new(pai386,
  2502. op_const_ref(A_MOV,S_L,0,hr)));
  2503. end;
  2504. exprasmlist^.concat(new(pai386,
  2505. op_reg(A_PUSH,S_L,R_EAX)));
  2506. end
  2507. else
  2508. begin
  2509. getlabel(again);
  2510. exprasmlist^.concat(new(pai386,
  2511. op_const_reg(A_MOV,S_L,stackframe div winstackpagesize,R_EDI)));
  2512. emitlab(again);
  2513. exprasmlist^.concat(new(pai386,
  2514. op_const_reg(A_SUB,S_L,winstackpagesize-4,R_ESP)));
  2515. exprasmlist^.concat(new(pai386,
  2516. op_reg(A_PUSH,S_L,R_EAX)));
  2517. exprasmlist^.concat(new(pai386,
  2518. op_reg(A_DEC,S_L,R_EDI)));
  2519. emitjmp(C_NZ,again);
  2520. exprasmlist^.concat(new(pai386,
  2521. op_const_reg(A_SUB,S_L,stackframe mod winstackpagesize,R_ESP)));
  2522. end
  2523. end
  2524. else
  2525. exprasmlist^.insert(new(pai386,op_const_reg(A_SUB,S_L,stackframe,R_ESP)));
  2526. if (cs_check_stack in aktlocalswitches) and
  2527. not(target_info.target in [target_i386_linux,target_i386_win32]) then
  2528. begin
  2529. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_STACKCHECK'))));
  2530. exprasmlist^.insert(new(pai386,op_const(A_PUSH,S_L,stackframe)));
  2531. end;
  2532. if cs_profile in aktmoduleswitches then
  2533. genprofilecode;
  2534. exprasmlist^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2535. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  2536. end;
  2537. end { endif stackframe <> 0 }
  2538. else
  2539. begin
  2540. if cs_profile in aktmoduleswitches then
  2541. genprofilecode;
  2542. exprasmlist^.insert(new(pai386,op_reg_reg(A_MOV,S_L,R_ESP,R_EBP)));
  2543. exprasmlist^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBP)));
  2544. end;
  2545. end;
  2546. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  2547. generate_interrupt_stackframe_entry;
  2548. { initialize return value }
  2549. if (procinfo.retdef<>pdef(voiddef)) and
  2550. (procinfo.retdef^.needs_inittable) and
  2551. ((procinfo.retdef^.deftype<>objectdef) or
  2552. not(pobjectdef(procinfo.retdef)^.isclass)) then
  2553. begin
  2554. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  2555. reset_reference(r);
  2556. r.offset:=procinfo.retoffset;
  2557. r.base:=procinfo.framepointer;
  2558. initialize(procinfo.retdef,r,ret_in_param(procinfo.retdef));
  2559. end;
  2560. { generate copies of call by value parameters }
  2561. if (aktprocsym^.definition^.options and poassembler=0) then
  2562. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}copyvalueparas);
  2563. { initialisizes local data }
  2564. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}initialize_data);
  2565. { add a reference to all call by value/const parameters }
  2566. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}incr_data);
  2567. { initilisizes temp. ansi/wide string data }
  2568. inittempansistrings;
  2569. { do we need an exception frame because of ansi/widestrings ? }
  2570. if (procinfo.flags and pi_needs_implicit_finally)<>0 then
  2571. begin
  2572. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  2573. { Type of stack-frame must be pushed}
  2574. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,1)));
  2575. emitcall('FPC_PUSHEXCEPTADDR');
  2576. exprasmlist^.concat(new(pai386,
  2577. op_reg(A_PUSH,S_L,R_EAX)));
  2578. emitcall('FPC_SETJMP');
  2579. exprasmlist^.concat(new(pai386,
  2580. op_reg(A_PUSH,S_L,R_EAX)));
  2581. exprasmlist^.concat(new(pai386,
  2582. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2583. emitjmp(C_NE,aktexitlabel);
  2584. end;
  2585. if (cs_profile in aktmoduleswitches) or
  2586. (aktprocsym^.definition^.owner^.symtabletype=globalsymtable) or
  2587. (assigned(procinfo._class) and (procinfo._class^.owner^.symtabletype=globalsymtable)) then
  2588. make_global:=true;
  2589. if not inlined then
  2590. begin
  2591. hs:=proc_names.get;
  2592. {$ifdef GDB}
  2593. if (cs_debuginfo in aktmoduleswitches) then
  2594. exprasmlist^.insert(new(pai_force_line,init));
  2595. if (cs_debuginfo in aktmoduleswitches) and target_os.use_function_relative_addresses then
  2596. stab_function_name := new(pai_stab_function_name,init(strpnew(hs)));
  2597. {$EndIf GDB}
  2598. while hs<>'' do
  2599. begin
  2600. if make_global then
  2601. exprasmlist^.insert(new(pai_symbol,initname_global(hs,0)))
  2602. else
  2603. exprasmlist^.insert(new(pai_symbol,initname(hs,0)));
  2604. {$ifdef GDB}
  2605. if (cs_debuginfo in aktmoduleswitches) and
  2606. target_os.use_function_relative_addresses then
  2607. exprasmlist^.insert(new(pai_stab_function_name,init(strpnew(hs))));
  2608. {$endif GDB}
  2609. hs:=proc_names.get;
  2610. end;
  2611. end;
  2612. {$ifdef GDB}
  2613. if (not inlined) and (cs_debuginfo in aktmoduleswitches) then
  2614. begin
  2615. if target_os.use_function_relative_addresses then
  2616. exprasmlist^.insert(stab_function_name);
  2617. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  2618. aktprocsym^.is_global := True;
  2619. exprasmlist^.insert(new(pai_stabs,init(aktprocsym^.stabstring)));
  2620. aktprocsym^.isstabwritten:=true;
  2621. end;
  2622. {$endif GDB}
  2623. { Align }
  2624. if (not inlined) then
  2625. begin
  2626. { gprof uses 16 byte granularity !! }
  2627. if (cs_profile in aktmoduleswitches) then
  2628. exprasmlist^.insert(new(pai_align,init_op(16,$90)))
  2629. else
  2630. if not(cs_littlesize in aktglobalswitches) then
  2631. exprasmlist^.insert(new(pai_align,init(4)));
  2632. end;
  2633. exprasmlist:=oldexprasmlist;
  2634. end;
  2635. procedure handle_return_value(inlined : boolean);
  2636. var
  2637. hr : preference;
  2638. op : Tasmop;
  2639. s : Topsize;
  2640. begin
  2641. if procinfo.retdef<>pdef(voiddef) then
  2642. begin
  2643. if ((procinfo.flags and pi_operator)<>0) and
  2644. assigned(opsym) then
  2645. procinfo.funcret_is_valid:=
  2646. procinfo.funcret_is_valid or (opsym^.refs>0);
  2647. if not(procinfo.funcret_is_valid) and not inlined { and
  2648. ((procinfo.flags and pi_uses_asm)=0)} then
  2649. CGMessage(sym_w_function_result_not_set);
  2650. hr:=new_reference(procinfo.framepointer,procinfo.retoffset);
  2651. if (procinfo.retdef^.deftype in [orddef,enumdef]) then
  2652. begin
  2653. case procinfo.retdef^.size of
  2654. 8:
  2655. begin
  2656. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)));
  2657. hr:=new_reference(procinfo.framepointer,procinfo.retoffset+4);
  2658. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EDX)));
  2659. end;
  2660. 4:
  2661. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)));
  2662. 2:
  2663. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,hr,R_AX)));
  2664. 1:
  2665. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_B,hr,R_AL)));
  2666. end;
  2667. end
  2668. else
  2669. if ret_in_acc(procinfo.retdef) then
  2670. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,R_EAX)))
  2671. else
  2672. if (procinfo.retdef^.deftype=floatdef) then
  2673. begin
  2674. floatloadops(pfloatdef(procinfo.retdef)^.typ,op,s);
  2675. exprasmlist^.concat(new(pai386,op_ref(op,s,hr)))
  2676. end
  2677. else
  2678. dispose(hr);
  2679. end
  2680. end;
  2681. procedure genexitcode(alist : paasmoutput;parasize:longint;nostackframe,inlined:boolean);
  2682. var
  2683. {$ifdef GDB}
  2684. mangled_length : longint;
  2685. p : pchar;
  2686. {$endif GDB}
  2687. noreraiselabel : pasmlabel;
  2688. hr : treference;
  2689. oldexprasmlist : paasmoutput;
  2690. begin
  2691. oldexprasmlist:=exprasmlist;
  2692. exprasmlist:=alist;
  2693. if aktexitlabel^.is_used then
  2694. exprasmlist^.insert(new(pai_label,init(aktexitlabel)));
  2695. { call the destructor help procedure }
  2696. if (aktprocsym^.definition^.options and podestructor)<>0 then
  2697. begin
  2698. if procinfo._class^.isclass then
  2699. begin
  2700. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,
  2701. newasmsymbol('FPC_DISPOSE_CLASS'))));
  2702. end
  2703. else
  2704. begin
  2705. exprasmlist^.insert(new(pai386,op_sym(A_CALL,S_NO,
  2706. newasmsymbol('FPC_HELP_DESTRUCTOR'))));
  2707. exprasmlist^.insert(new(pai386,op_const_reg(A_MOV,S_L,procinfo._class^.vmt_offset,R_EDI)));
  2708. end;
  2709. end;
  2710. { finalize temporary data }
  2711. finalizetempansistrings;
  2712. { finalize local data }
  2713. aktprocsym^.definition^.localst^.foreach({$ifndef TP}@{$endif}finalize_data);
  2714. { finalize paras data }
  2715. if assigned(aktprocsym^.definition^.parast) then
  2716. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}finalize_data);
  2717. { do we need to handle exceptions because of ansi/widestrings ? }
  2718. if (procinfo.flags and pi_needs_implicit_finally)<>0 then
  2719. begin
  2720. getlabel(noreraiselabel);
  2721. exprasmlist^.concat(new(pai386,
  2722. op_sym(A_CALL,S_NO,newasmsymbol('FPC_POPADDRSTACK'))));
  2723. exprasmlist^.concat(new(pai386,
  2724. op_reg(A_POP,S_L,R_EAX)));
  2725. exprasmlist^.concat(new(pai386,
  2726. op_reg_reg(A_TEST,S_L,R_EAX,R_EAX)));
  2727. emitjmp(C_E,noreraiselabel);
  2728. { must be the return value finalized before reraising the exception? }
  2729. if (procinfo.retdef<>pdef(voiddef)) and
  2730. (procinfo.retdef^.needs_inittable) and
  2731. ((procinfo.retdef^.deftype<>objectdef) or
  2732. not(pobjectdef(procinfo.retdef)^.isclass)) then
  2733. begin
  2734. reset_reference(hr);
  2735. hr.offset:=procinfo.retoffset;
  2736. hr.base:=procinfo.framepointer;
  2737. finalize(procinfo.retdef,hr,ret_in_param(procinfo.retdef));
  2738. end;
  2739. exprasmlist^.concat(new(pai386,
  2740. op_sym(A_CALL,S_NO,newasmsymbol('FPC_RERAISE'))));
  2741. exprasmlist^.concat(new(pai_label,init(noreraiselabel)));
  2742. end;
  2743. { call __EXIT for main program }
  2744. if (not DLLsource) and (not inlined) and ((aktprocsym^.definition^.options and poproginit)<>0) then
  2745. begin
  2746. exprasmlist^.concat(new(pai386,op_sym(A_CALL,S_NO,newasmsymbol('FPC_DO_EXIT'))));
  2747. end;
  2748. { handle return value }
  2749. if (aktprocsym^.definition^.options and poassembler)=0 then
  2750. if (aktprocsym^.definition^.options and poconstructor)=0 then
  2751. handle_return_value(inlined)
  2752. else
  2753. begin
  2754. { successful constructor deletes the zero flag }
  2755. { and returns self in eax }
  2756. exprasmlist^.concat(new(pai_label,init(quickexitlabel)));
  2757. { eax must be set to zero if the allocation failed !!! }
  2758. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_ESI,R_EAX)));
  2759. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,R_EAX,R_EAX)));
  2760. end;
  2761. { stabs uses the label also ! }
  2762. if aktexit2label^.is_used or
  2763. ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  2764. exprasmlist^.concat(new(pai_label,init(aktexit2label)));
  2765. { gives problems for long mangled names }
  2766. {list^.concat(new(pai_symbol,init(aktprocsym^.definition^.mangledname+'_end')));}
  2767. { should we restore edi ? }
  2768. { for all i386 gcc implementations }
  2769. if ((aktprocsym^.definition^.options and posavestdregs)<>0) then
  2770. begin
  2771. if (aktprocsym^.definition^.usedregisters and ($80 shr byte(R_EBX)))<>0 then
  2772. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  2773. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  2774. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  2775. { here we could reset R_EBX
  2776. but that is risky because it only works
  2777. if genexitcode is called after genentrycode
  2778. so lets skip this for the moment PM
  2779. aktprocsym^.definition^.usedregisters:=
  2780. aktprocsym^.definition^.usedregisters or not ($80 shr byte(R_EBX));
  2781. }
  2782. end;
  2783. if not(nostackframe) and not inlined then
  2784. exprasmlist^.concat(new(pai386,op_none(A_LEAVE,S_NO)));
  2785. { parameters are limited to 65535 bytes because }
  2786. { ret allows only imm16 }
  2787. if (parasize>65535) and not(aktprocsym^.definition^.options and poclearstack<>0) then
  2788. CGMessage(cg_e_parasize_too_big);
  2789. { at last, the return is generated }
  2790. if not inlined then
  2791. if (aktprocsym^.definition^.options and pointerrupt)<>0 then
  2792. generate_interrupt_stackframe_exit
  2793. else
  2794. begin
  2795. {Routines with the poclearstack flag set use only a ret.}
  2796. { also routines with parasize=0 }
  2797. if (parasize=0) or (aktprocsym^.definition^.options and poclearstack<>0) then
  2798. exprasmlist^.concat(new(pai386,op_none(A_RET,S_NO)))
  2799. else
  2800. exprasmlist^.concat(new(pai386,op_const(A_RET,S_NO,parasize)));
  2801. end;
  2802. exprasmlist^.concat(new(pai_symbol_end,initname(aktprocsym^.definition^.mangledname)));
  2803. {$ifdef GDB}
  2804. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  2805. begin
  2806. aktprocsym^.concatstabto(exprasmlist);
  2807. if assigned(procinfo._class) then
  2808. if (not assigned(procinfo.parent) or
  2809. not assigned(procinfo.parent^._class)) then
  2810. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2811. '"$t:v'+procinfo._class^.numberstring+'",'+
  2812. tostr(N_PSYM)+',0,0,'+tostr(procinfo.esi_offset)))))
  2813. else
  2814. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2815. '"$t:r'+procinfo._class^.numberstring+'",'+
  2816. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[R_ESI])))));
  2817. if (pdef(aktprocsym^.definition^.retdef) <> pdef(voiddef)) then
  2818. if ret_in_param(aktprocsym^.definition^.retdef) then
  2819. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2820. '"'+aktprocsym^.name+':X*'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  2821. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))))
  2822. else
  2823. exprasmlist^.concat(new(pai_stabs,init(strpnew(
  2824. '"'+aktprocsym^.name+':X'+aktprocsym^.definition^.retdef^.numberstring+'",'+
  2825. tostr(N_PSYM)+',0,0,'+tostr(procinfo.retoffset)))));
  2826. mangled_length:=length(aktprocsym^.definition^.mangledname);
  2827. getmem(p,mangled_length+50);
  2828. strpcopy(p,'192,0,0,');
  2829. strpcopy(strend(p),aktprocsym^.definition^.mangledname);
  2830. exprasmlist^.concat(new(pai_stabn,init(strnew(p))));
  2831. {list^.concat(new(pai_stabn,init(strpnew('192,0,0,'
  2832. +aktprocsym^.definition^.mangledname))));
  2833. p[0]:='2';p[1]:='2';p[2]:='4';
  2834. strpcopy(strend(p),'_end');}
  2835. freemem(p,mangled_length+50);
  2836. exprasmlist^.concat(new(pai_stabn,init(
  2837. strpnew('224,0,0,'+aktexit2label^.name))));
  2838. { strpnew('224,0,0,'
  2839. +aktprocsym^.definition^.mangledname+'_end'))));}
  2840. end;
  2841. {$endif GDB}
  2842. exprasmlist:=oldexprasmlist;
  2843. end;
  2844. {$ifdef test_dest_loc}
  2845. procedure mov_reg_to_dest(p : ptree; s : topsize; reg : tregister);
  2846. begin
  2847. if (dest_loc.loc=LOC_CREGISTER) or (dest_loc.loc=LOC_REGISTER) then
  2848. begin
  2849. emit_reg_reg(A_MOV,s,reg,dest_loc.register);
  2850. set_location(p^.location,dest_loc);
  2851. in_dest_loc:=true;
  2852. end
  2853. else
  2854. if (dest_loc.loc=LOC_REFERENCE) or (dest_loc.loc=LOC_MEM) then
  2855. begin
  2856. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,s,reg,newreference(dest_loc.reference))));
  2857. set_location(p^.location,dest_loc);
  2858. in_dest_loc:=true;
  2859. end
  2860. else
  2861. internalerror(20080);
  2862. end;
  2863. {$endif test_dest_loc}
  2864. end.
  2865. {
  2866. $Log$
  2867. Revision 1.19 1999-07-29 20:53:58 peter
  2868. * write .size also
  2869. Revision 1.18 1999/07/26 12:13:46 florian
  2870. * exit in try..finally blocks needed a second fix
  2871. * a raise in a try..finally lead into a endless loop, fixed
  2872. Revision 1.17 1999/07/22 09:37:38 florian
  2873. + resourcestring implemented
  2874. + start of longstring support
  2875. Revision 1.16 1999/07/18 10:41:59 florian
  2876. * fix of my previous commit nevertheless it doesn't work completly
  2877. Revision 1.15 1999/07/18 10:19:44 florian
  2878. * made it compilable with Dlephi 4 again
  2879. + fixed problem with large stack allocations on win32
  2880. Revision 1.14 1999/07/06 21:48:11 florian
  2881. * a lot bug fixes:
  2882. - po_external isn't any longer necessary for procedure compatibility
  2883. - m_tp_procvar is in -Sd now available
  2884. - error messages of procedure variables improved
  2885. - return values with init./finalization fixed
  2886. - data types with init./finalization aren't any longer allowed in variant
  2887. record
  2888. Revision 1.13 1999/07/05 20:25:22 peter
  2889. * merged
  2890. Revision 1.12 1999/07/05 20:13:13 peter
  2891. * removed temp defines
  2892. Revision 1.11 1999/07/05 11:56:56 jonas
  2893. * merged
  2894. Revision 1.5.2.5 1999/07/05 20:03:31 peter
  2895. * removed warning/notes
  2896. Revision 1.5.2.3 1999/07/04 21:50:17 jonas
  2897. * everything between $ifdef jmpfix:
  2898. * when a jxx instruction is disposed, decrease the refcount of the label
  2899. it referenced
  2900. * for jmp instructions to a label, set is_jmp also to true (was only done
  2901. for Jcc instructions)
  2902. Revision 1.9 1999/07/01 15:49:11 florian
  2903. * int64/qword type release
  2904. + lo/hi for int64/qword
  2905. Revision 1.8 1999/06/28 22:29:15 florian
  2906. * qword division fixed
  2907. + code for qword/int64 type casting added:
  2908. range checking isn't implemented yet
  2909. Revision 1.7 1999/06/17 13:19:50 pierre
  2910. * merged from 0_99_12 branch
  2911. Revision 1.5.2.2 1999/06/17 12:38:39 pierre
  2912. * wrong warning for operators removed
  2913. Revision 1.6 1999/06/14 17:47:48 peter
  2914. * merged
  2915. Revision 1.5.2.1 1999/06/14 17:27:08 peter
  2916. * fixed posavestd regs which popped at the wrong place
  2917. Revision 1.5 1999/06/03 16:21:15 pierre
  2918. * fixes a bug due to int64 code in maybe_savetotemp
  2919. Revision 1.4 1999/06/02 22:44:06 pierre
  2920. * previous wrong log corrected
  2921. Revision 1.3 1999/06/02 22:25:29 pierre
  2922. * changed $ifdef FPC @ into $ifndef TP
  2923. Revision 1.2 1999/06/02 10:11:49 florian
  2924. * make cycle fixed i.e. compilation with 0.99.10
  2925. * some fixes for qword
  2926. * start of register calling conventions
  2927. Revision 1.1 1999/06/01 19:33:18 peter
  2928. * reinserted
  2929. Revision 1.158 1999/06/01 14:45:46 peter
  2930. * @procvar is now always needed for FPC
  2931. Revision 1.157 1999/05/27 19:44:20 peter
  2932. * removed oldasm
  2933. * plabel -> pasmlabel
  2934. * -a switches to source writing automaticly
  2935. * assembler readers OOPed
  2936. * asmsymbol automaticly external
  2937. * jumptables and other label fixes for asm readers
  2938. Revision 1.156 1999/05/24 08:55:24 florian
  2939. * non working safecall directiv implemented, I don't know if we
  2940. need it
  2941. Revision 1.155 1999/05/23 19:55:14 florian
  2942. * qword/int64 multiplication fixed
  2943. + qword/int64 subtraction
  2944. Revision 1.154 1999/05/23 18:42:05 florian
  2945. * better error recovering in typed constants
  2946. * some problems with arrays of const fixed, some problems
  2947. due my previous
  2948. - the location type of array constructor is now LOC_MEM
  2949. - the pushing of high fixed
  2950. - parameter copying fixed
  2951. - zero temp. allocation removed
  2952. * small problem in the assembler writers fixed:
  2953. ref to nil wasn't written correctly
  2954. Revision 1.153 1999/05/21 13:54:55 peter
  2955. * NEWLAB for label as symbol
  2956. Revision 1.152 1999/05/19 22:00:45 florian
  2957. * some new routines for register management:
  2958. maybe_savetotemp,restorefromtemp, saveusedregisters,
  2959. restoreusedregisters
  2960. Revision 1.151 1999/05/19 20:40:11 florian
  2961. * fixed a couple of array related bugs:
  2962. - var a : array[0..1] of char; p : pchar; p:=a+123; works now
  2963. - open arrays with an odd size doesn't work: movsb wasn't generated
  2964. - introduced some new array type helper routines (is_special_array) etc.
  2965. - made the array type checking in isconvertable more strict, often
  2966. open array can be used where is wasn't allowed etc...
  2967. Revision 1.150 1999/05/19 15:26:30 florian
  2968. * if a non local variables isn't initialized the compiler doesn't write
  2969. any longer "local var. seems not to be ..."
  2970. Revision 1.149 1999/05/19 13:59:07 jonas
  2971. * no more "enter" generated when -Og is used (caused sometimes crashes under
  2972. Linux, don't know why)
  2973. Revision 1.148 1999/05/18 21:58:30 florian
  2974. * fixed some bugs related to temp. ansistrings and functions results
  2975. which return records/objects/arrays which need init/final.
  2976. Revision 1.147 1999/05/18 14:15:28 peter
  2977. * containsself fixes
  2978. * checktypes()
  2979. Revision 1.146 1999/05/17 22:42:26 florian
  2980. * FPC_ANSISTR_DECR_REF needs a reference!
  2981. Revision 1.145 1999/05/17 21:57:06 florian
  2982. * new temporary ansistring handling
  2983. Revision 1.144 1999/05/15 21:33:18 peter
  2984. * redesigned temp_gen temp allocation so temp allocation for
  2985. ansistring works correct. It also does a best fit instead of first fit
  2986. Revision 1.143 1999/05/13 21:59:22 peter
  2987. * removed oldppu code
  2988. * warning if objpas is loaded from uses
  2989. * first things for new deref writing
  2990. Revision 1.142 1999/05/12 00:19:46 peter
  2991. * removed R_DEFAULT_SEG
  2992. * uniform float names
  2993. Revision 1.141 1999/05/07 00:33:44 pierre
  2994. explicit type conv to pobject checked with cond TESTOBJEXT2
  2995. Revision 1.140 1999/05/06 09:05:17 peter
  2996. * generic write_float and str_float
  2997. * fixed constant float conversions
  2998. Revision 1.139 1999/05/04 21:44:35 florian
  2999. * changes to compile it with Delphi 4.0
  3000. Revision 1.138 1999/05/02 09:35:36 florian
  3001. + method message handlers which contain an explicit self can't be called
  3002. directly anymore
  3003. + self is now loaded at the start of the an message handler with an explicit
  3004. self
  3005. + $useoverlay fixed: i386 was renamed to i386base
  3006. Revision 1.137 1999/05/01 13:24:16 peter
  3007. * merged nasm compiler
  3008. * old asm moved to oldasm/
  3009. Revision 1.136 1999/04/28 06:01:56 florian
  3010. * changes of Bruessel:
  3011. + message handler can now take an explicit self
  3012. * typinfo fixed: sometimes the type names weren't written
  3013. * the type checking for pointer comparisations and subtraction
  3014. and are now more strict (was also buggy)
  3015. * small bug fix to link.pas to support compiling on another
  3016. drive
  3017. * probable bug in popt386 fixed: call/jmp => push/jmp
  3018. transformation didn't count correctly the jmp references
  3019. + threadvar support
  3020. * warning if ln/sqrt gets an invalid constant argument
  3021. Revision 1.135 1999/04/26 13:31:26 peter
  3022. * release storenumber,double_checksum
  3023. Revision 1.134 1999/04/21 21:53:08 pierre
  3024. * previous log corrected
  3025. Revision 1.133 1999/04/21 16:31:38 pierre
  3026. + TEMPS_NOT_PUSH conditionnal code :
  3027. put needed registers into temp space instead of pushing them
  3028. Revision 1.132 1999/04/21 09:43:30 peter
  3029. * storenumber works
  3030. * fixed some typos in double_checksum
  3031. + incompatible types type1 and type2 message (with storenumber)
  3032. Revision 1.131 1999/04/19 09:45:49 pierre
  3033. + cdecl or stdcall push all args with longint size
  3034. * tempansi stuff cleaned up
  3035. Revision 1.130 1999/04/17 13:14:50 peter
  3036. * concat_external added for new init/final
  3037. Revision 1.129 1999/04/16 20:44:35 florian
  3038. * the boolean operators =;<>;xor with LOC_JUMP and LOC_FLAGS
  3039. operands fixed, small things for new ansistring management
  3040. Revision 1.128 1999/04/16 13:42:31 jonas
  3041. * more regalloc fixes (still not complete)
  3042. Revision 1.127 1999/04/16 10:28:23 pierre
  3043. + added posavestdregs used for cdecl AND stdcall functions
  3044. (saves ESI EDI and EBX for i386)
  3045. Revision 1.126 1999/04/16 09:56:06 pierre
  3046. * unused local var commented
  3047. Revision 1.125 1999/04/15 13:08:30 pierre
  3048. * misplaced statement in concatcopy corrected
  3049. Revision 1.124 1999/04/15 12:19:55 peter
  3050. + finalization support
  3051. Revision 1.123 1999/04/09 00:00:52 pierre
  3052. + uses ungetiftempansi
  3053. Revision 1.122 1999/04/08 15:57:47 peter
  3054. + subrange checking for readln()
  3055. Revision 1.121 1999/03/31 13:55:08 peter
  3056. * assembler inlining working for ag386bin
  3057. Revision 1.120 1999/03/26 00:05:27 peter
  3058. * released valintern
  3059. + deffile is now removed when compiling is finished
  3060. * ^( compiles now correct
  3061. + static directive
  3062. * shrd fixed
  3063. Revision 1.119 1999/03/24 23:16:55 peter
  3064. * fixed bugs 212,222,225,227,229,231,233
  3065. Revision 1.118 1999/03/16 17:52:49 jonas
  3066. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  3067. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  3068. * in cgai386: also small fixes to emitrangecheck
  3069. Revision 1.117 1999/03/09 19:29:12 peter
  3070. * ecxpushed was not reset in concatcopy
  3071. Revision 1.116 1999/03/09 11:45:40 pierre
  3072. * small arrays and records (size <=4) are copied directly
  3073. Revision 1.115 1999/03/03 12:15:13 pierre
  3074. * U_SYSWIN32_ISCONSOLE adde to external list
  3075. Revision 1.114 1999/03/02 18:21:33 peter
  3076. + flags support for add and case
  3077. Revision 1.113 1999/03/01 15:46:19 peter
  3078. * ag386bin finally make cycles correct
  3079. * prefixes are now also normal opcodes
  3080. Revision 1.112 1999/03/01 13:39:44 pierre
  3081. * temp for int_value const parameters
  3082. Revision 1.111 1999/02/25 21:02:32 peter
  3083. * ag386bin updates
  3084. + coff writer
  3085. Revision 1.110 1999/02/22 02:15:17 peter
  3086. * updates for ag386bin
  3087. Revision 1.109 1999/02/16 00:46:09 peter
  3088. * optimized concatcopy with ecx=1 and ecx=0
  3089. Revision 1.108 1999/02/15 13:13:14 pierre
  3090. * fix for bug0216
  3091. Revision 1.107 1999/02/12 10:43:58 florian
  3092. * internal error 10 with ansistrings fixed
  3093. Revision 1.106 1999/02/03 09:50:22 pierre
  3094. * conditionnal code to try to release temp for consts that are not in memory
  3095. Revision 1.105 1999/02/02 11:47:56 peter
  3096. * fixed ansi2short
  3097. Revision 1.104 1999/01/25 09:29:36 florian
  3098. * very rare problem with in-operator fixed, mainly it was a problem of
  3099. emit_to_reg32 (typo in case ranges)
  3100. Revision 1.103 1999/01/21 22:10:42 peter
  3101. * fixed array of const
  3102. * generic platform independent high() support
  3103. Revision 1.102 1999/01/19 10:19:00 florian
  3104. * bug with mul. of dwords fixed, reported by Alexander Stohr
  3105. * some changes to compile with TP
  3106. + small enhancements for the new code generator
  3107. Revision 1.101 1999/01/15 11:36:48 pierre
  3108. * double temp disallocation on ansistring removed
  3109. Revision 1.100 1998/12/30 13:41:08 peter
  3110. * released valuepara
  3111. Revision 1.99 1998/12/22 13:11:00 florian
  3112. * memory leaks for ansistring type casts fixed
  3113. Revision 1.98 1998/12/19 00:23:46 florian
  3114. * ansistring memory leaks fixed
  3115. Revision 1.97 1998/12/11 16:10:08 florian
  3116. + shifting for 64 bit ints added
  3117. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  3118. Revision 1.96 1998/12/11 00:03:11 peter
  3119. + globtype,tokens,version unit splitted from globals
  3120. Revision 1.95 1998/12/10 09:47:19 florian
  3121. + basic operations with int64/qord (compiler with -dint64)
  3122. + rtti of enumerations extended: names are now written
  3123. Revision 1.94 1998/12/03 10:17:27 peter
  3124. * target_os.use_bound_instruction boolean
  3125. Revision 1.93 1998/11/30 19:48:56 peter
  3126. * some more rangecheck fixes
  3127. Revision 1.92 1998/11/30 16:34:44 pierre
  3128. * corrected problems with rangecheck
  3129. + added needed code for no rangecheck in CRC32 functions in ppu unit
  3130. * enumdef lso need its rangenr reset to zero
  3131. when calling reset_global_defs
  3132. Revision 1.91 1998/11/30 09:43:07 pierre
  3133. * some range check bugs fixed (still not working !)
  3134. + added DLL writing support for win32 (also accepts variables)
  3135. + TempAnsi for code that could be used for Temporary ansi strings
  3136. handling
  3137. Revision 1.90 1998/11/29 12:43:45 peter
  3138. * commented the fpc_init_stack_check becuase it is not in the RTL
  3139. Revision 1.89 1998/11/27 14:50:34 peter
  3140. + open strings, $P switch support
  3141. Revision 1.88 1998/11/26 21:33:07 peter
  3142. * rangecheck updates
  3143. Revision 1.87 1998/11/26 13:10:41 peter
  3144. * new int - int conversion -dNEWCNV
  3145. * some function renamings
  3146. Revision 1.86 1998/11/26 09:53:37 florian
  3147. * for classes no init/final. code is necessary, fixed
  3148. Revision 1.85 1998/11/20 15:35:56 florian
  3149. * problems with rtti fixed, hope it works
  3150. Revision 1.84 1998/11/18 17:45:25 peter
  3151. * fixes for VALUEPARA
  3152. Revision 1.83 1998/11/18 15:44:12 peter
  3153. * VALUEPARA for tp7 compatible value parameters
  3154. Revision 1.82 1998/11/17 00:36:41 peter
  3155. * more ansistring fixes
  3156. Revision 1.81 1998/11/16 19:23:33 florian
  3157. * isconsole is now set by win32 applications
  3158. Revision 1.80 1998/11/16 15:35:40 peter
  3159. * rename laod/copystring -> load/copyshortstring
  3160. * fixed int-bool cnv bug
  3161. + char-ansistring conversion
  3162. Revision 1.79 1998/11/16 11:28:56 pierre
  3163. * stackcheck removed for i386_win32
  3164. * exportlist does not crash at least !!
  3165. (was need for tests dir !)z
  3166. Revision 1.78 1998/11/15 16:32:34 florian
  3167. * some stuff of Pavel implement (win32 dll creation)
  3168. * bug with ansistring function results fixed
  3169. Revision 1.77 1998/11/13 15:40:17 pierre
  3170. + added -Se in Makefile cvstest target
  3171. + lexlevel cleanup
  3172. normal_function_level main_program_level and unit_init_level defined
  3173. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3174. (test added in code !)
  3175. * -Un option was wrong
  3176. * _FAIL and _SELF only keyword inside
  3177. constructors and methods respectively
  3178. Revision 1.76 1998/11/12 16:43:33 florian
  3179. * functions with ansi strings as result didn't work, solved
  3180. Revision 1.75 1998/11/12 11:19:44 pierre
  3181. * fix for first line of function break
  3182. Revision 1.74 1998/11/12 09:46:18 pierre
  3183. + break main stops before calls to unit inits
  3184. + break at constructors stops before call to FPC_NEW_CLASS
  3185. or FPC_HELP_CONSTRUCTOR
  3186. Revision 1.73 1998/11/10 10:50:55 pierre
  3187. * temporary fix for long mangled procsym names
  3188. Revision 1.72 1998/11/05 12:02:40 peter
  3189. * released useansistring
  3190. * removed -Sv, its now available in fpc modes
  3191. Revision 1.71 1998/10/29 15:42:45 florian
  3192. + partial disposing of temp. ansistrings
  3193. Revision 1.70 1998/10/25 23:32:49 peter
  3194. * fixed unsigned mul
  3195. Revision 1.69 1998/10/20 13:11:33 peter
  3196. + def_getreg to get a register with the same size as definition
  3197. Revision 1.68 1998/10/20 08:06:48 pierre
  3198. * several memory corruptions due to double freemem solved
  3199. => never use p^.loc.location:=p^.left^.loc.location;
  3200. + finally I added now by default
  3201. that ra386dir translates global and unit symbols
  3202. + added a first field in tsymtable and
  3203. a nextsym field in tsym
  3204. (this allows to obtain ordered type info for
  3205. records and objects in gdb !)
  3206. Revision 1.67 1998/10/16 13:12:50 pierre
  3207. * added vmt_offsets in destructors code also !!!
  3208. * vmt_offset code for m68k
  3209. Revision 1.66 1998/10/16 08:48:40 peter
  3210. * fixed some misplaced $endif GDB
  3211. Revision 1.65 1998/10/15 12:37:40 pierre
  3212. + passes vmt offset to HELP_CONSTRUCTOR for objects
  3213. Revision 1.64 1998/10/13 16:50:13 pierre
  3214. * undid some changes of Peter that made the compiler wrong
  3215. for m68k (I had to reinsert some ifdefs)
  3216. * removed several memory leaks under m68k
  3217. * removed the meory leaks for assembler readers
  3218. * cross compiling shoud work again better
  3219. ( crosscompiling sysamiga works
  3220. but as68k still complain about some code !)
  3221. Revision 1.63 1998/10/13 13:10:13 peter
  3222. * new style for m68k/i386 infos and enums
  3223. Revision 1.62 1998/10/08 17:17:17 pierre
  3224. * current_module old scanner tagged as invalid if unit is recompiled
  3225. + added ppheap for better info on tracegetmem of heaptrc
  3226. (adds line column and file index)
  3227. * several memory leaks removed ith help of heaptrc !!
  3228. Revision 1.61 1998/10/08 13:48:41 peter
  3229. * fixed memory leaks for do nothing source
  3230. * fixed unit interdependency
  3231. Revision 1.60 1998/10/07 10:37:43 peter
  3232. * fixed stabs
  3233. Revision 1.59 1998/10/06 17:16:45 pierre
  3234. * some memory leaks fixed (thanks to Peter for heaptrc !)
  3235. Revision 1.58 1998/10/05 21:33:16 peter
  3236. * fixed 161,165,166,167,168
  3237. Revision 1.57 1998/10/01 09:22:54 peter
  3238. * fixed value openarray
  3239. * ungettemp of arrayconstruct
  3240. Revision 1.56 1998/09/28 16:57:19 pierre
  3241. * changed all length(p^.value_str^) into str_length(p)
  3242. to get it work with and without ansistrings
  3243. * changed sourcefiles field of tmodule to a pointer
  3244. Revision 1.55 1998/09/28 16:18:15 florian
  3245. * two fixes to get ansi strings work
  3246. Revision 1.54 1998/09/20 17:46:49 florian
  3247. * some things regarding ansistrings fixed
  3248. Revision 1.53 1998/09/20 09:38:44 florian
  3249. * hasharray for defs fixed
  3250. * ansistring code generation corrected (init/final, assignement)
  3251. Revision 1.52 1998/09/17 09:42:31 peter
  3252. + pass_2 for cg386
  3253. * Message() -> CGMessage() for pass_1/pass_2
  3254. Revision 1.51 1998/09/14 10:44:05 peter
  3255. * all internal RTL functions start with FPC_
  3256. Revision 1.50 1998/09/07 18:46:01 peter
  3257. * update smartlinking, uses getdatalabel
  3258. * renamed ptree.value vars to value_str,value_real,value_set
  3259. Revision 1.49 1998/09/05 22:10:52 florian
  3260. + switch -vb
  3261. * while/repeat loops accept now also word/longbool conditions
  3262. * makebooltojump did an invalid ungetregister32, fixed
  3263. Revision 1.48 1998/09/04 08:41:52 peter
  3264. * updated some error CGMessages
  3265. Revision 1.47 1998/09/03 17:08:41 pierre
  3266. * better lines for stabs
  3267. (no scroll back to if before else part
  3268. no return to case line at jump outside case)
  3269. + source lines also if not in order
  3270. Revision 1.46 1998/09/03 16:03:16 florian
  3271. + rtti generation
  3272. * init table generation changed
  3273. Revision 1.45 1998/09/01 12:48:03 peter
  3274. * use pdef^.size instead of orddef^.typ
  3275. Revision 1.44 1998/09/01 09:07:11 peter
  3276. * m68k fixes, splitted cg68k like cgi386
  3277. Revision 1.43 1998/08/21 08:40:52 pierre
  3278. * EBX,EDI,ESI saved for CDECL on all i386 targets
  3279. Revision 1.42 1998/08/19 16:07:41 jonas
  3280. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  3281. Revision 1.41 1998/08/19 00:40:43 peter
  3282. * small crash prevention
  3283. Revision 1.40 1998/08/17 10:10:06 peter
  3284. - removed OLDPPU
  3285. Revision 1.39 1998/08/15 16:51:39 peter
  3286. * save also esi,ebx for cdecl procedures
  3287. Revision 1.38 1998/08/14 18:18:42 peter
  3288. + dynamic set contruction
  3289. * smallsets are now working (always longint size)
  3290. Revision 1.37 1998/08/11 00:00:30 peter
  3291. * fixed dup log
  3292. Revision 1.36 1998/08/10 14:49:52 peter
  3293. + localswitches, moduleswitches, globalswitches splitting
  3294. Revision 1.35 1998/08/05 16:00:11 florian
  3295. * some fixes for ansi strings
  3296. Revision 1.34 1998/07/30 11:18:14 florian
  3297. + first implementation of try ... except on .. do end;
  3298. * limitiation of 65535 bytes parameters for cdecl removed
  3299. Revision 1.33 1998/07/27 21:57:12 florian
  3300. * fix to allow tv like stream registration:
  3301. @tmenu.load doesn't work if load had parameters or if load was only
  3302. declared in an anchestor class of tmenu
  3303. Revision 1.32 1998/07/27 11:23:40 florian
  3304. + procedures with the directive cdecl and with target linux save now
  3305. the register EDI (like GCC procedures).
  3306. Revision 1.31 1998/07/20 18:40:11 florian
  3307. * handling of ansi string constants should now work
  3308. Revision 1.30 1998/07/18 22:54:26 florian
  3309. * some ansi/wide/longstring support fixed:
  3310. o parameter passing
  3311. o returning as result from functions
  3312. Revision 1.29 1998/07/06 13:21:13 michael
  3313. + Fixed Initialization/Finalizarion calls
  3314. Revision 1.28 1998/06/25 08:48:11 florian
  3315. * first version of rtti support
  3316. Revision 1.27 1998/06/24 14:48:32 peter
  3317. * ifdef newppu -> ifndef oldppu
  3318. Revision 1.26 1998/06/16 08:56:19 peter
  3319. + targetcpu
  3320. * cleaner pmodules for newppu
  3321. Revision 1.25 1998/06/08 13:13:40 pierre
  3322. + temporary variables now in temp_gen.pas unit
  3323. because it is processor independent
  3324. * mppc68k.bat modified to undefine i386 and support_mmx
  3325. (which are defaults for i386)
  3326. Revision 1.24 1998/06/07 15:30:23 florian
  3327. + first working rtti
  3328. + data init/final. for local variables
  3329. Revision 1.23 1998/06/05 17:49:53 peter
  3330. * cleanup of cgai386
  3331. Revision 1.22 1998/06/04 09:55:34 pierre
  3332. * demangled name of procsym reworked to become
  3333. independant of the mangling scheme
  3334. Revision 1.21 1998/06/03 22:48:51 peter
  3335. + wordbool,longbool
  3336. * rename bis,von -> high,low
  3337. * moved some systemunit loading/creating to psystem.pas
  3338. Revision 1.20 1998/05/30 14:31:03 peter
  3339. + $ASMMODE
  3340. Revision 1.19 1998/05/23 01:21:02 peter
  3341. + aktasmmode, aktoptprocessor, aktoutputformat
  3342. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3343. + $LIBNAME to set the library name where the unit will be put in
  3344. * splitted cgi386 a bit (codeseg to large for bp7)
  3345. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3346. Revision 1.18 1998/05/20 09:42:32 pierre
  3347. + UseTokenInfo now default
  3348. * unit in interface uses and implementation uses gives error now
  3349. * only one error for unknown symbol (uses lastsymknown boolean)
  3350. the problem came from the label code !
  3351. + first inlined procedures and function work
  3352. (warning there might be allowed cases were the result is still wrong !!)
  3353. * UseBrower updated gives a global list of all position of all used symbols
  3354. with switch -gb
  3355. Revision 1.17 1998/05/11 13:07:53 peter
  3356. + $ifdef NEWPPU for the new ppuformat
  3357. + $define GDB not longer required
  3358. * removed all warnings and stripped some log comments
  3359. * no findfirst/findnext anymore to remove smartlink *.o files
  3360. Revision 1.16 1998/05/07 00:17:00 peter
  3361. * smartlinking for sets
  3362. + consts labels are now concated/generated in hcodegen
  3363. * moved some cpu code to cga and some none cpu depended code from cga
  3364. to tree and hcodegen and cleanup of hcodegen
  3365. * assembling .. output reduced for smartlinking ;)
  3366. Revision 1.15 1998/05/06 08:38:35 pierre
  3367. * better position info with UseTokenInfo
  3368. UseTokenInfo greatly simplified
  3369. + added check for changed tree after first time firstpass
  3370. (if we could remove all the cases were it happen
  3371. we could skip all firstpass if firstpasscount > 1)
  3372. Only with ExtDebug
  3373. Revision 1.14 1998/05/04 17:54:24 peter
  3374. + smartlinking works (only case jumptable left todo)
  3375. * redesign of systems.pas to support assemblers and linkers
  3376. + Unitname is now also in the PPU-file, increased version to 14
  3377. Revision 1.13 1998/05/01 16:38:43 florian
  3378. * handling of private and protected fixed
  3379. + change_keywords_to_tp implemented to remove
  3380. keywords which aren't supported by tp
  3381. * break and continue are now symbols of the system unit
  3382. + widestring, longstring and ansistring type released
  3383. Revision 1.12 1998/05/01 07:43:52 florian
  3384. + basics for rtti implemented
  3385. + switch $m (generate rtti for published sections)
  3386. Revision 1.11 1998/04/29 13:41:17 peter
  3387. + assembler functions are not profiled
  3388. Revision 1.10 1998/04/29 10:33:47 pierre
  3389. + added some code for ansistring (not complete nor working yet)
  3390. * corrected operator overloading
  3391. * corrected nasm output
  3392. + started inline procedures
  3393. + added starstarn : use ** for exponentiation (^ gave problems)
  3394. + started UseTokenInfo cond to get accurate positions
  3395. Revision 1.9 1998/04/21 10:16:46 peter
  3396. * patches from strasbourg
  3397. * objects is not used anymore in the fpc compiled version
  3398. Revision 1.8 1998/04/13 08:42:50 florian
  3399. * call by reference and call by value open arrays fixed
  3400. Revision 1.7 1998/04/09 23:27:26 peter
  3401. * fixed profiling results
  3402. Revision 1.6 1998/04/09 14:28:03 jonas
  3403. + basic k6 and 6x86 optimizing support (-O7 and -O8)
  3404. Revision 1.5 1998/04/08 16:58:01 pierre
  3405. * several bugfixes
  3406. ADD ADC and AND are also sign extended
  3407. nasm output OK (program still crashes at end
  3408. and creates wrong assembler files !!)
  3409. procsym types sym in tdef removed !!
  3410. }