12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865 |
- {
- $Id$
- Copyright (c) 1993-98 by Florian Klaempfl
- Generate i386 assembler for in call nodes
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit cg386cal;
- interface
- uses
- symtable,tree;
- { save the size of pushed parameter }
- var
- pushedparasize : longint;
- procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
- push_from_left_to_right,inlined : boolean;para_offset : longint);
- procedure secondcalln(var p : ptree);
- procedure secondprocinline(var p : ptree);
- implementation
- uses
- globtype,systems,
- cobjects,verbose,globals,
- aasm,types,
- {$ifdef GDB}
- gdb,
- {$endif GDB}
- hcodegen,temp_gen,pass_2,
- i386,cgai386,tgeni386,cg386ld;
- {*****************************************************************************
- SecondCallParaN
- *****************************************************************************}
- procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
- push_from_left_to_right,inlined : boolean;para_offset : longint);
- function push_addr(p:ptree):boolean;
- begin
- push_addr:=(
- {$ifndef VALUEPARA}
- dont_copy_const_param(p^.resulttype) { or }
- {$else}
- push_addr_param(p^.resulttype)
- {$endif}
- { hmmm, why this ?? (FK) or ((p^.treetype=stringconstn) and is_ansistring(p^.resulttype)) }
- );
- end;
- procedure maybe_push_high;
- var
- r : preference;
- hreg : tregister;
- href : treference;
- len : longint;
- begin
- { open array ? }
- { defcoll^.data can be nil for read/write }
- if assigned(defcoll^.data) and
- (is_open_array(defcoll^.data) or
- is_open_string(defcoll^.data)) then
- begin
- { push high }
- case p^.left^.resulttype^.deftype of
- arraydef : begin
- if is_open_array(p^.left^.resulttype) then
- begin
- r:=new_reference(highframepointer,highoffset+4);
- len:=-1;
- end
- else
- len:=parraydef(p^.left^.resulttype)^.highrange-
- parraydef(p^.left^.resulttype)^.lowrange
- end;
- stringdef : begin
- if is_open_string(defcoll^.data) then
- begin
- if is_open_string(p^.left^.resulttype) then
- begin
- r:=new_reference(highframepointer,highoffset+4);
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
- hreg:=R_EDI;
- len:=-2;
- end
- else
- len:=pstringdef(p^.left^.resulttype)^.len
- end
- else
- { passing a string to an array of char }
- begin
- if (p^.left^.treetype=stringconstn) then
- len:=str_length(p^.left)
- else
- begin
- href:=p^.left^.location.reference;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVZX,S_BL,newreference(href),R_EDI)));
- hreg:=R_EDI;
- len:=-2;
- end;
- end;
- end;
- else
- len:=0;
- end;
- { Push from the reference? }
- if len=-1 then
- begin
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)));
- end
- else
- { Push from a register? }
- if len=-2 then
- begin
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hreg)));
- ungetregister32(hreg);
- end
- else
- { Push direct value }
- begin
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,len,r)));
- end
- else
- push_int(len);
- end;
- inc(pushedparasize,4);
- end;
- end;
- var
- size : longint;
- {$ifndef VALUEPARA}
- stackref : treference;
- {$endif}
- otlabel,hlabel,oflabel : plabel;
- { temporary variables: }
- tempdeftype : tdeftype;
- tempreference : treference;
- r : preference;
- opsize : topsize;
- op : tasmop;
- hreg : tregister;
- begin
- { push from left to right if specified }
- if push_from_left_to_right and assigned(p^.right) then
- secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
- otlabel:=truelabel;
- oflabel:=falselabel;
- getlabel(truelabel);
- getlabel(falselabel);
- secondpass(p^.left);
- { filter array constructor with c styled args }
- if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
- begin
- { nothing, everything is already pushed }
- end
- { in codegen.handleread.. defcoll^.data is set to nil }
- else if assigned(defcoll^.data) and
- (defcoll^.data^.deftype=formaldef) then
- begin
- { allow @var }
- inc(pushedparasize,4);
- if p^.left^.treetype=addrn then
- begin
- { always a register }
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
- p^.left^.location.register,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
- ungetregister32(p^.left^.location.register);
- end
- else
- begin
- if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
- CGMessage(type_e_mismatch)
- else
- begin
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.left^.location.reference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
- del_reference(p^.left^.location.reference);
- end;
- end;
- end
- { handle call by reference parameter }
- else if (defcoll^.paratyp=vs_var) then
- begin
- if (p^.left^.location.loc<>LOC_REFERENCE) then
- CGMessage(cg_e_var_must_be_reference);
- maybe_push_high;
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.left^.location.reference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
- del_reference(p^.left^.location.reference);
- end
- else
- begin
- tempdeftype:=p^.resulttype^.deftype;
- if tempdeftype=filedef then
- CGMessage(cg_e_file_must_call_by_reference);
- if
- {$ifndef VALUEPARA}
- (defcoll^.paratyp=vs_const) and
- {$endif}
- push_addr(p^.left) then
- begin
- maybe_push_high;
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.left^.location.reference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
- R_EDI,r)));
- end
- else
- emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
- del_reference(p^.left^.location.reference);
- end
- else
- case p^.left^.location.loc of
- LOC_REGISTER,
- LOC_CREGISTER:
- begin
- case p^.left^.location.register of
- R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
- R_EDI,R_ESP,R_EBP :
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
- p^.left^.location.register,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
- ungetregister32(p^.left^.location.register);
- end;
- R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
- begin
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- hreg:=reg16toreg32(p^.left^.location.register);
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=p^.left^.location.register;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
- ungetregister32(reg16toreg32(p^.left^.location.register));
- end;
- R_AL,R_BL,R_CL,R_DL:
- begin
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- hreg:=reg8toreg32(p^.left^.location.register);
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=reg8toreg16(p^.left^.location.register);
- inc(pushedparasize,2);
- end;
- { we must push always 16 bit }
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
- ungetregister32(reg8toreg32(p^.left^.location.register));
- end;
- end;
- end;
- LOC_FPU:
- begin
- size:=align(pfloatdef(p^.left^.resulttype)^.size,target_os.stackalignment);
- inc(pushedparasize,size);
- if not inlined then
- exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- r:=new_reference(R_ESP,0);
- floatstoreops(pfloatdef(p^.left^.resulttype)^.typ,op,opsize);
- { this is the easiest case for inlined !! }
- if inlined then
- begin
- r^.base:=procinfo.framepointer;
- r^.offset:=para_offset-pushedparasize;
- end;
- exprasmlist^.concat(new(pai386,op_ref(op,opsize,r)));
- end;
- LOC_REFERENCE,LOC_MEM:
- begin
- tempreference:=p^.left^.location.reference;
- del_reference(p^.left^.location.reference);
- case p^.resulttype^.deftype of
- enumdef,
- orddef :
- begin
- case p^.resulttype^.size of
- 8 : begin
- inc(pushedparasize,8);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- inc(tempreference.offset,4);
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize+4);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- begin
- inc(tempreference.offset,4);
- emit_push_mem(tempreference);
- dec(tempreference.offset,4);
- emit_push_mem(tempreference);
- end;
- end;
- 4 : begin
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- 1,2 : begin
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EDI;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_DI;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
- newreference(tempreference),hreg)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
- newreference(tempreference))));
- end;
- else
- internalerror(234231);
- end;
- end;
- floatdef :
- begin
- case pfloatdef(p^.resulttype)^.typ of
- f32bit,
- s32real :
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- s64real,
- s64bit :
- begin
- inc(pushedparasize,4);
- inc(tempreference.offset,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- inc(pushedparasize,4);
- dec(tempreference.offset,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- s80real :
- begin
- inc(pushedparasize,4);
- if target_os.stackalignment=4 then
- inc(tempreference.offset,8)
- else
- inc(tempreference.offset,6);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- dec(tempreference.offset,4);
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EDI;
- inc(pushedparasize,4);
- dec(tempreference.offset,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_DI;
- inc(pushedparasize,2);
- dec(tempreference.offset,2);
- end;
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
- newreference(tempreference),hreg)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_ref(A_PUSH,opsize,
- newreference(tempreference))));
- end;
- end;
- end;
- pointerdef,procvardef,
- classrefdef:
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(tempreference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
- end
- else
- emit_push_mem(tempreference);
- end;
- arraydef,recorddef,stringdef,setdef,objectdef :
- begin
- { even some structured types are 32 bit }
- if is_widestring(p^.resulttype) or
- is_ansistring(p^.resulttype) or
- is_smallset(p^.resulttype) or
- ((p^.resulttype^.deftype=objectdef) and
- pobjectdef(p^.resulttype)^.isclass) then
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- concatcopy(tempreference,r^,4,false,false);
- end
- else
- emit_push_mem(tempreference);
- end
- { call by value open array ? }
- else
- {$ifndef VALUEPARA}
- if (p^.resulttype^.deftype=arraydef) and
- assigned(defcoll^.data) and
- is_open_array(defcoll^.data) then
- begin
- { first, push high }
- maybe_push_high;
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.left^.location.reference),R_EDI)));
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
- R_EDI,r)));
- end
- else
- emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
- end
- else
- begin
- size:=align(p^.resulttype^.size,target_os.stackalignment);
- { create stack space }
- if not inlined then
- exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- inc(pushedparasize,size);
- { create stack reference }
- stackref.symbol := nil;
- if not inlined then
- begin
- clear_reference(stackref);
- stackref.base:=R_ESP;
- end
- else
- begin
- clear_reference(stackref);
- stackref.base:=procinfo.framepointer;
- stackref.offset:=para_offset-pushedparasize;
- end;
- { generate copy }
- if is_shortstring(p^.resulttype) then
- begin
- copyshortstring(stackref,p^.left^.location.reference,
- pstringdef(p^.resulttype)^.len,false);
- end
- else
- begin
- concatcopy(p^.left^.location.reference,
- stackref,p^.resulttype^.size,true,false);
- end;
- end;
- {$else VALUEPARA}
- begin
- internalerror(8954);
- end;
- {$endif VALUEPARA}
- end;
- else
- CGMessage(cg_e_illegal_expression);
- end;
- end;
- LOC_JUMP:
- begin
- getlabel(hlabel);
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- inc(pushedparasize,2);
- end;
- emitl(A_LABEL,truelabel);
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,1,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,1)));
- emitl(A_JMP,hlabel);
- emitl(A_LABEL,falselabel);
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,0,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_const(A_PUSH,opsize,0)));
- emitl(A_LABEL,hlabel);
- end;
- LOC_FLAGS:
- begin
- if not(R_EAX in unused) then
- exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));
- exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
- R_AL)));
- exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BW,R_AL,R_AX)));
- if target_os.stackalignment=4 then
- begin
- opsize:=S_L;
- hreg:=R_EAX;
- inc(pushedparasize,4);
- end
- else
- begin
- opsize:=S_W;
- hreg:=R_AX;
- inc(pushedparasize,2);
- end;
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,hreg,r)));
- end
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,opsize,hreg)));
- if not(R_EAX in unused) then
- exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EDI,R_EAX)));
- end;
- {$ifdef SUPPORT_MMX}
- LOC_MMXREGISTER,
- LOC_CMMXREGISTER:
- begin
- inc(pushedparasize,8); { was missing !!! (PM) }
- exprasmlist^.concat(new(pai386,op_const_reg(
- A_SUB,S_L,8,R_ESP)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- if inlined then
- begin
- r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
- p^.left^.location.register,r)));
- end
- else
- begin
- r:=new_reference(R_ESP,0);
- exprasmlist^.concat(new(pai386,op_reg_ref(
- A_MOVQ,S_NO,p^.left^.location.register,r)));
- end;
- end;
- {$endif SUPPORT_MMX}
- end;
- end;
- freelabel(truelabel);
- freelabel(falselabel);
- truelabel:=otlabel;
- falselabel:=oflabel;
- { push from right to left }
- if not push_from_left_to_right and assigned(p^.right) then
- secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
- end;
- {*****************************************************************************
- SecondCallN
- *****************************************************************************}
- procedure secondcalln(var p : ptree);
- var
- unusedregisters : tregisterset;
- pushed : tpushed;
- hr,funcretref : treference;
- hregister,hregister2 : tregister;
- oldpushedparasize : longint;
- { true if ESI must be loaded again after the subroutine }
- loadesi : boolean;
- { true if a virtual method must be called directly }
- no_virtual_call : boolean;
- { true if we produce a con- or destrutor in a call }
- is_con_or_destructor : boolean;
- { true if a constructor is called again }
- extended_new : boolean;
- { adress returned from an I/O-error }
- iolabel : plabel;
- { lexlevel count }
- i : longint;
- { help reference pointer }
- r : preference;
- hp,
- pp,params : ptree;
- inlined : boolean;
- inlinecode : ptree;
- para_offset : longint;
- { instruction for alignement correction }
- { corr : pai386;}
- { we must pop this size also after !! }
- { must_pop : boolean; }
- pop_size : longint;
- label
- dont_call;
- begin
- extended_new:=false;
- iolabel:=nil;
- inlinecode:=nil;
- inlined:=false;
- loadesi:=true;
- no_virtual_call:=false;
- unusedregisters:=unused;
- if not assigned(p^.procdefinition) then
- exit;
- if (p^.procdefinition^.options and poinline)<>0 then
- begin
- inlined:=true;
- inlinecode:=p^.right;
- { set it to the same lexical level }
- p^.procdefinition^.parast^.symtablelevel:=
- aktprocsym^.definition^.parast^.symtablelevel;
- if assigned(p^.left) then
- inlinecode^.para_offset:=
- gettempofsizepersistant(inlinecode^.para_size);
- p^.procdefinition^.parast^.call_offset:=
- inlinecode^.para_offset;
- {$ifdef extdebug}
- Comment(V_debug,
- 'inlined parasymtable is at offset '
- +tostr(p^.procdefinition^.parast^.call_offset));
- exprasmlist^.concat(new(pai_asm_comment,init(
- strpnew('inlined parasymtable is at offset '
- +tostr(p^.procdefinition^.parast^.call_offset)))));
- {$endif extdebug}
- p^.right:=nil;
- { disable further inlining of the same proc
- in the args }
- p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
- end;
- { only if no proc var }
- if not(assigned(p^.right)) then
- is_con_or_destructor:=((p^.procdefinition^.options and poconstructor)<>0)
- or ((p^.procdefinition^.options and podestructor)<>0);
- { proc variables destroy all registers }
- if (p^.right=nil) and
- { virtual methods too }
- ((p^.procdefinition^.options and povirtualmethod)=0) then
- begin
- if ((p^.procdefinition^.options and poiocheck)<>0) and
- ((aktprocsym^.definition^.options and poiocheck)=0) and
- (cs_check_io in aktlocalswitches) then
- begin
- getlabel(iolabel);
- emitl(A_LABEL,iolabel);
- end
- else
- iolabel:=nil;
- { save all used registers }
- pushusedregisters(pushed,p^.procdefinition^.usedregisters);
- { give used registers through }
- usedinproc:=usedinproc or p^.procdefinition^.usedregisters;
- end
- else
- begin
- pushusedregisters(pushed,$ff);
- usedinproc:=$ff;
- { no IO check for methods and procedure variables }
- iolabel:=nil;
- end;
- { generate the code for the parameter and push them }
- oldpushedparasize:=pushedparasize;
- pushedparasize:=0;
- pop_size:=0;
- if (not inlined) then
- begin
- { Old pushedsize aligned on 4 ? }
- i:=oldpushedparasize and 3;
- if i>0 then
- inc(pop_size,4-i);
- { This parasize aligned on 4 ? }
- i:=p^.procdefinition^.para_size and 3;
- if i>0 then
- inc(pop_size,4-i);
- { insert the opcode and update pushedparasize }
- if pop_size>0 then
- begin
- inc(pushedparasize,pop_size);
- exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,pop_size,R_ESP)));
- {$ifdef GDB}
- if (cs_debuginfo in aktmoduleswitches) and
- (exprasmlist^.first=exprasmlist^.last) then
- exprasmlist^.concat(new(pai_force_line,init));
- {$endif GDB}
- end;
- end;
- if (p^.resulttype<>pdef(voiddef)) and
- ret_in_param(p^.resulttype) then
- begin
- funcretref.symbol:=nil;
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) and
- (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
- begin
- funcretref:=dest_loc.reference;
- if assigned(dest_loc.reference.symbol) then
- funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
- in_dest_loc:=true;
- end
- else
- {$endif test_dest_loc}
- if inlined then
- begin
- reset_reference(funcretref);
- funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.retdef^.size);
- funcretref.base:=procinfo.framepointer;
- end
- else
- gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
- end;
- if assigned(p^.left) then
- begin
- { be found elsewhere }
- if inlined then
- para_offset:=p^.procdefinition^.parast^.call_offset+
- p^.procdefinition^.parast^.datasize
- else
- para_offset:=0;
- if assigned(p^.right) then
- secondcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1,
- (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset)
- else
- secondcallparan(p^.left,p^.procdefinition^.para1,
- (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset);
- end;
- params:=p^.left;
- p^.left:=nil;
- if inlined then
- inlinecode^.retoffset:=gettempofsizepersistant(4);
- if ret_in_param(p^.resulttype) then
- begin
- inc(pushedparasize,4);
- if inlined then
- begin
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(funcretref),R_EDI)));
- r:=new_reference(procinfo.framepointer,inlinecode^.retoffset);
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
- R_EDI,r)));
- end
- else
- emitpushreferenceaddr(exprasmlist,funcretref);
- end;
- { procedure variable ? }
- if (p^.right=nil) then
- begin
- { overloaded operator have no symtable }
- { push self }
- if assigned(p^.symtable) and
- (p^.symtable^.symtabletype=withsymtable) then
- begin
- { dirty trick to avoid the secondcall below }
- p^.methodpointer:=genzeronode(callparan);
- p^.methodpointer^.location.loc:=LOC_REGISTER;
- p^.methodpointer^.location.register:=R_ESI;
- p^.methodpointer^.resulttype:=p^.symtable^.defowner;
- { change dispose type !! }
- p^.disposetyp:=dt_mbleft_and_method;
- { make a reference }
- new(r);
- reset_reference(r^);
- r^.offset:=p^.symtable^.datasize;
- r^.base:=procinfo.framepointer;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
- end;
- { push self }
- if assigned(p^.symtable) and
- ((p^.symtable^.symtabletype=objectsymtable) or
- (p^.symtable^.symtabletype=withsymtable)) then
- begin
- if assigned(p^.methodpointer) then
- begin
- {
- if p^.methodpointer^.resulttype=classrefdef then
- begin
- two possibilities:
- 1. constructor
- 2. class method
- end
- else }
- begin
- case p^.methodpointer^.treetype of
- typen:
- begin
- { direct call to inherited method }
- if (p^.procdefinition^.options and poabstractmethod)<>0 then
- begin
- CGMessage(cg_e_cant_call_abstract_method);
- goto dont_call;
- end;
- { generate no virtual call }
- no_virtual_call:=true;
- if (p^.symtableprocentry^.properties and sp_static)<>0 then
- begin
- { well lets put the VMT address directly into ESI }
- { it is kind of dirty but that is the simplest }
- { way to accept virtual static functions (PM) }
- loadesi:=true;
- exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,S_L,
- newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0),R_ESI)));
- maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
- pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- end
- else
- { this is a member call, so ESI isn't modfied }
- loadesi:=false;
- if not(is_con_or_destructor and
- pobjectdef(p^.methodpointer^.resulttype)^.isclass and
- assigned(aktprocsym) and
- ((aktprocsym^.definition^.options and
- (poconstructor or podestructor))<>0)) then
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- { if an inherited con- or destructor should be }
- { called in a con- or destructor then a warning }
- { will be made }
- { con- and destructors need a pointer to the vmt }
- if is_con_or_destructor and
- not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) and
- assigned(aktprocsym) then
- begin
- if not ((aktprocsym^.definition^.options
- and (poconstructor or podestructor))<>0) then
- CGMessage(cg_w_member_cd_call_from_method);
- end;
- if is_con_or_destructor then
- push_int(0)
- end;
- hnewn:
- begin
- { extended syntax of new }
- { ESI must be zero }
- exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- { insert the vmt }
- exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
- newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
- maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
- pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
- extended_new:=true;
- end;
- hdisposen:
- begin
- secondpass(p^.methodpointer);
- { destructor with extended syntax called from dispose }
- { hdisposen always deliver LOC_REFERENCE }
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.methodpointer^.location.reference),R_ESI)));
- del_reference(p^.methodpointer^.location.reference);
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
- newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
- maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
- pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
- end;
- else
- begin
- { call to an instance member }
- if (p^.symtable^.symtabletype<>withsymtable) then
- begin
- secondpass(p^.methodpointer);
- case p^.methodpointer^.location.loc of
- LOC_CREGISTER,
- LOC_REGISTER:
- begin
- ungetregister32(p^.methodpointer^.location.register);
- emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
- end;
- else
- begin
- if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
- ((p^.methodpointer^.resulttype^.deftype=objectdef) and
- pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
- newreference(p^.methodpointer^.location.reference),R_ESI)))
- else
- exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
- newreference(p^.methodpointer^.location.reference),R_ESI)));
- del_reference(p^.methodpointer^.location.reference);
- end;
- end;
- end;
- { when calling a class method, we have
- to load ESI with the VMT !
- But that's wrong, if we call a class method via self
- }
- if ((p^.procdefinition^.options and poclassmethod)<>0)
- and not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
- begin
- { class method needs current VMT }
- new(r);
- reset_reference(r^);
- r^.base:=R_ESI;
- r^.offset:= p^.procdefinition^._class^.vmt_offset;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
- end;
- { direct call to class constructor, don't allocate memory }
- if is_con_or_destructor and
- (p^.methodpointer^.resulttype^.deftype=objectdef) and
- (pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
- exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,0)))
- else
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- if is_con_or_destructor then
- begin
- { classes don't get a VMT pointer pushed }
- if (p^.methodpointer^.resulttype^.deftype=objectdef) and
- not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
- begin
- if ((p^.procdefinition^.options and poconstructor)<>0) then
- begin
- { it's no bad idea, to insert the VMT }
- exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
- newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,
- 0))));
- maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
- pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
- end
- { destructors haven't to dispose the instance, if this is }
- { a direct call }
- else
- push_int(0);
- end;
- end;
- end;
- end;
- end;
- end
- else
- begin
- if ((p^.procdefinition^.options and poclassmethod)<>0) and
- not(
- assigned(aktprocsym) and
- ((aktprocsym^.definition^.options and poclassmethod)<>0)
- ) then
- begin
- { class method needs current VMT }
- new(r);
- reset_reference(r^);
- r^.base:=R_ESI;
- r^.offset:= p^.procdefinition^._class^.vmt_offset;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
- end
- else
- begin
- { member call, ESI isn't modified }
- loadesi:=false;
- end;
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- { but a con- or destructor here would probably almost }
- { always be placed wrong }
- if is_con_or_destructor then
- begin
- CGMessage(cg_w_member_cd_call_from_method);
- push_int(0);
- end;
- end;
- end;
- { push base pointer ?}
- if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
- ((p^.procdefinition^.parast^.symtablelevel)>normal_function_level) then
- begin
- { if we call a nested function in a method, we must }
- { push also SELF! }
- { THAT'S NOT TRUE, we have to load ESI via frame pointer }
- { access }
- {
- begin
- loadesi:=false;
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
- end;
- }
- if lexlevel=(p^.procdefinition^.parast^.symtablelevel) then
- begin
- new(r);
- reset_reference(r^);
- r^.offset:=procinfo.framepointer_offset;
- r^.base:=procinfo.framepointer;
- exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)))
- end
- { this is only true if the difference is one !!
- but it cannot be more !! }
- else if (lexlevel=p^.procdefinition^.parast^.symtablelevel-1) then
- begin
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,procinfo.framepointer)))
- end
- else if (lexlevel>p^.procdefinition^.parast^.symtablelevel) then
- begin
- hregister:=getregister32;
- new(r);
- reset_reference(r^);
- r^.offset:=procinfo.framepointer_offset;
- r^.base:=procinfo.framepointer;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
- for i:=(p^.procdefinition^.parast^.symtablelevel) to lexlevel-1 do
- begin
- new(r);
- reset_reference(r^);
- {we should get the correct frame_pointer_offset at each level
- how can we do this !!! }
- r^.offset:=procinfo.framepointer_offset;
- r^.base:=hregister;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
- end;
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
- ungetregister32(hregister);
- end
- else
- internalerror(25000);
- end;
- if ((p^.procdefinition^.options and povirtualmethod)<>0) and
- not(no_virtual_call) then
- begin
- { static functions contain the vmt_address in ESI }
- { also class methods }
- if assigned(aktprocsym) then
- begin
- if ((aktprocsym^.properties and sp_static)<>0) or
- ((aktprocsym^.definition^.options and poclassmethod)<>0) or
- ((p^.procdefinition^.options and postaticmethod)<>0) or
- ((p^.procdefinition^.options and poconstructor)<>0) or
- { ESI is loaded earlier }
- ((p^.procdefinition^.options and poclassmethod)<>0)then
- begin
- new(r);
- reset_reference(r^);
- r^.base:=R_ESI;
- end
- else
- begin
- new(r);
- reset_reference(r^);
- r^.base:=R_ESI;
- { this is one point where we need vmt_offset (PM) }
- r^.offset:= p^.procdefinition^._class^.vmt_offset;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
- new(r);
- reset_reference(r^);
- r^.base:=R_EDI;
- end;
- end
- else
- { aktprocsym should be assigned, also in main program }
- internalerror(12345);
- {
- begin
- new(r);
- reset_reference(r^);
- r^.base:=R_ESI;
- exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
- new(r);
- reset_reference(r^);
- r^.base:=R_EDI;
- end;
- }
- if p^.procdefinition^.extnumber=-1 then
- internalerror($Da);
- r^.offset:=p^.procdefinition^.extnumber*4+12;
- if (cs_check_range in aktlocalswitches) then
- begin
- exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
- emitcall('FPC_CHECK_OBJECT',true);
- end;
- exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,r)));
- end
- else if not inlined then
- emitcall(p^.procdefinition^.mangledname,
- (p^.symtableproc^.symtabletype=unitsymtable) or
- ((p^.symtableproc^.symtabletype=objectsymtable) and
- (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable))or
- ((p^.symtableproc^.symtabletype=withsymtable) and
- (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable)))
- else { inlined proc }
- { inlined code is in inlinecode }
- begin
- secondpass(inlinecode);
- { set poinline again }
- p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
- { free the args }
- ungetpersistanttemp(p^.procdefinition^.parast^.call_offset,
- p^.procdefinition^.parast^.datasize);
- end;
- end
- else
- { now procedure variable case }
- begin
- secondpass(p^.right);
- { method pointer ? }
- if (p^.procdefinition^.options and pomethodpointer)<>0 then
- begin
- { method pointer can't be in a register }
- inc(p^.right^.location.reference.offset,4);
- { push self pointer }
- exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.right^.location.reference))));
- del_reference(p^.right^.location.reference);
- dec(p^.right^.location.reference.offset,4);
- end;
- case p^.right^.location.loc of
- LOC_REGISTER,LOC_CREGISTER:
- begin
- exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,p^.right^.location.register)));
- ungetregister32(p^.right^.location.register);
- end
- else
- exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))));
- del_reference(p^.right^.location.reference);
- end;
- end;
- { this was only for normal functions
- displaced here so we also get
- it to work for procvars PM }
- if (not inlined) and ((p^.procdefinition^.options and poclearstack)<>0) then
- begin
- { consider the alignment with the rest (PM) }
- inc(pushedparasize,pop_size);
- pop_size:=0;
- { better than an add on all processors }
- if pushedparasize=4 then
- exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)))
- { the pentium has two pipes and pop reg is pairable }
- { but the registers must be different! }
- else if (pushedparasize=8) and
- not(cs_littlesize in aktglobalswitches) and
- (aktoptprocessor=ClassP5) and
- (procinfo._class=nil) then
- begin
- exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
- exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
- end
- else exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pushedparasize,R_ESP)));
- end;
- dont_call:
- pushedparasize:=oldpushedparasize;
- unused:=unusedregisters;
- { handle function results }
- { structured results are easy to handle.... }
- { needed also when result_no_used !! }
- if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
- begin
- p^.location.loc:=LOC_MEM;
- stringdispose(p^.location.reference.symbol);
- p^.location.reference:=funcretref;
- end;
- if (p^.resulttype<>pdef(voiddef)) and p^.return_value_used then
- begin
- { a contructor could be a function with boolean result }
- if (p^.right=nil) and
- ((p^.procdefinition^.options and poconstructor)<>0) and
- { quick'n'dirty check if it is a class or an object }
- (p^.resulttype^.deftype=orddef) then
- begin
- p^.location.loc:=LOC_FLAGS;
- p^.location.resflags:=F_NE;
- if extended_new then
- begin
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_L,R_EAX)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
- p^.location.register:=hregister;
- end;
- end;
- end
- { structed results are easy to handle.... }
- else if ret_in_param(p^.resulttype) then
- begin
- {p^.location.loc:=LOC_MEM;
- stringdispose(p^.location.reference.symbol);
- p^.location.reference:=funcretref;
- already done above (PM) }
- end
- else
- begin
- if (p^.resulttype^.deftype=orddef) then
- begin
- p^.location.loc:=LOC_REGISTER;
- case porddef(p^.resulttype)^.typ of
- s32bit,u32bit,bool32bit :
- begin
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_L,R_EAX)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
- p^.location.register:=hregister;
- end;
- end;
- uchar,u8bit,bool8bit,s8bit:
- begin
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_B,R_AL)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
- p^.location.register:=reg32toreg8(hregister);
- end;
- end;
- s16bit,u16bit,bool16bit :
- begin
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_W,R_AX)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
- p^.location.register:=reg32toreg16(hregister);
- end;
- end;
- s64bitint,u64bit:
- begin
- {$ifdef test_dest_loc}
- {$error Don't know what to do here}
- {$endif test_dest_loc}
- hregister:=getexplicitregister32(R_EAX);
- hregister2:=getexplicitregister32(R_EDX);
- emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
- emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
- p^.location.registerlow:=hregister;
- p^.location.registerhigh:=hregister2;
- end;
- else internalerror(7);
- end
- end
- else if (p^.resulttype^.deftype=floatdef) then
- case pfloatdef(p^.resulttype)^.typ of
- f32bit:
- begin
- p^.location.loc:=LOC_REGISTER;
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_L,R_EAX)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
- p^.location.register:=hregister;
- end;
- end;
- else
- p^.location.loc:=LOC_FPU;
- end
- else
- begin
- p^.location.loc:=LOC_REGISTER;
- {$ifdef test_dest_loc}
- if dest_loc_known and (dest_loc_tree=p) then
- mov_reg_to_dest(p,S_L,R_EAX)
- else
- {$endif test_dest_loc}
- begin
- hregister:=getexplicitregister32(R_EAX);
- emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
- p^.location.register:=hregister;
- if is_ansistring(p^.resulttype) or
- is_widestring(p^.resulttype) then
- begin
- gettempansistringreference(hr);
- {temptoremove^.concat(new(ptemptodestroy,init(hr,p^.resulttype)));}
- exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,
- newreference(hr))));
- end;
- end;
- end;
- end;
- end;
- { perhaps i/o check ? }
- if iolabel<>nil then
- begin
- exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
- emitcall('FPC_IOCHECK',true);
- end;
- if pop_size>0 then
- exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pop_size,R_ESP)));
- { restore registers }
- popusedregisters(pushed);
- { at last, restore instance pointer (SELF) }
- if loadesi then
- maybe_loadesi;
- pp:=params;
- while assigned(pp) do
- begin
- if assigned(pp^.left) then
- begin
- if pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
- ungetiftemp(pp^.left^.location.reference);
- { process also all nodes of an array of const }
- if pp^.left^.treetype=arrayconstructn then
- begin
- if assigned(pp^.left^.left) then
- begin
- hp:=pp^.left;
- while assigned(hp) do
- begin
- if hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
- ungetiftemp(hp^.left^.location.reference);
- hp:=hp^.right;
- end;
- end;
- end;
- end;
- pp:=pp^.right;
- end;
- if inlined then
- ungetpersistanttemp(inlinecode^.retoffset,4);
- disposetree(params);
- { from now on the result can be freed normally }
- if inlined and ret_in_param(p^.resulttype) then
- persistanttemptonormal(funcretref.offset);
- { if return value is not used }
- if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
- begin
- if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
- { release unused temp }
- ungetiftemp(p^.location.reference)
- else if p^.location.loc=LOC_FPU then
- { release FPU stack }
- exprasmlist^.concat(new(pai386,op_none(A_FDECSTP,S_NO)));
- end;
- end;
- {*****************************************************************************
- SecondProcInlineN
- *****************************************************************************}
- { implementation not complete yet }
- var
- addr_correction : longint;
- procedure correct_address(p : psym);{$ifndef FPC}far;{$endif}
- begin
- if p^.typ=varsym then
- begin
- inc(pvarsym(p)^.address,addr_correction);
- {$ifdef extdebug}
- Comment(V_debug,pvarsym(p)^.name+' is at offset -'
- +tostr(pvarsym(p)^.address));
- exprasmlist^.concat(new(pai_asm_comment,init(
- strpnew(pvarsym(p)^.name+' is at offset -'
- +tostr(pvarsym(p)^.address)))));
- {$endif extdebug}
- end;
- end;
- procedure secondprocinline(var p : ptree);
- var st : psymtable;
- oldprocsym : pprocsym;
- para_size : longint;
- oldprocinfo : tprocinfo;
- { just dummies for genentrycode }
- nostackframe,make_global : boolean;
- proc_names : tstringcontainer;
- inlineentrycode,inlineexitcode : paasmoutput;
- oldexitlabel,oldexit2label,oldquickexitlabel:Plabel;
- begin
- oldexitlabel:=aktexitlabel;
- oldexit2label:=aktexit2label;
- oldquickexitlabel:=quickexitlabel;
- getlabel(aktexitlabel);
- getlabel(aktexit2label);
- oldprocsym:=aktprocsym;
- oldprocinfo:=procinfo;
- { set the return value }
- procinfo.retdef:=p^.inlineprocdef^.retdef;
- procinfo.retoffset:=p^.retoffset;
- { arg space has been filled by the parent secondcall }
- st:=p^.inlineprocdef^.localst;
- { set it to the same lexical level }
- st^.symtablelevel:=
- oldprocsym^.definition^.localst^.symtablelevel;
- if st^.datasize>0 then
- st^.call_offset:=gettempofsizepersistant(st^.datasize);
- {$ifdef extdebug}
- Comment(V_debug,'local symtable is at offset '
- +tostr(st^.call_offset));
- exprasmlist^.concat(new(pai_asm_comment,init(
- strpnew('local symtable is at offset '
- +tostr(st^.call_offset)))));
- {$endif extdebug}
- addr_correction:=-st^.call_offset-st^.datasize;
- st^.foreach(correct_address);
- {$ifdef extdebug}
- exprasmlist^.concat(new(pai_asm_comment,init('Start of inlined proc')));
- {$endif extdebug}
- { takes care of local data initialization }
- inlineentrycode:=new(paasmoutput,init);
- inlineexitcode:=new(paasmoutput,init);
- proc_names.init;
- para_size:=p^.para_size;
- make_global:=false; { to avoid warning }
- genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
- exprasmlist^.concatlist(inlineentrycode);
- secondpass(p^.left);
- genexitcode(inlineexitcode,0,false,true);
- exprasmlist^.concatlist(inlineexitcode);
- {$ifdef extdebug}
- exprasmlist^.concat(new(pai_asm_comment,init('End of inlined proc')));
- {$endif extdebug}
- {we can free the local data now }
- if st^.datasize>0 then
- ungetpersistanttemp(st^.call_offset,st^.datasize);
- { set the real address again }
- addr_correction:=-addr_correction;
- st^.foreach(correct_address);
- aktprocsym:=oldprocsym;
- freelabel(aktexitlabel);
- freelabel(aktexit2label);
- aktexitlabel:=oldexitlabel;
- aktexit2label:=oldexit2label;
- quickexitlabel:=oldquickexitlabel;
- procinfo:=oldprocinfo;
- end;
- end.
- {
- $Log$
- Revision 1.53 1998-12-11 00:02:47 peter
- + globtype,tokens,version unit splitted from globals
- Revision 1.52 1998/12/10 14:39:29 florian
- * bug with p(const a : ansistring) fixed
- * duplicate constant ansistrings were handled wrong, fixed
- Revision 1.51 1998/12/10 09:47:15 florian
- + basic operations with int64/qord (compiler with -dint64)
- + rtti of enumerations extended: names are now written
- Revision 1.50 1998/12/06 13:12:44 florian
- * better code generation for classes which are passed as parameters to
- subroutines
- Revision 1.49 1998/11/30 09:43:00 pierre
- * some range check bugs fixed (still not working !)
- + added DLL writing support for win32 (also accepts variables)
- + TempAnsi for code that could be used for Temporary ansi strings
- handling
- Revision 1.48 1998/11/27 14:50:30 peter
- + open strings, $P switch support
- Revision 1.47 1998/11/26 21:30:03 peter
- * fix for valuepara
- Revision 1.46 1998/11/26 14:39:10 peter
- * ansistring -> pchar fixed
- * ansistring constants fixed
- * ansistring constants are now written once
- Revision 1.45 1998/11/18 15:44:07 peter
- * VALUEPARA for tp7 compatible value parameters
- Revision 1.44 1998/11/16 15:35:36 peter
- * rename laod/copystring -> load/copyshortstring
- * fixed int-bool cnv bug
- + char-ansistring conversion
- Revision 1.43 1998/11/15 16:32:33 florian
- * some stuff of Pavel implement (win32 dll creation)
- * bug with ansistring function results fixed
- Revision 1.42 1998/11/13 15:40:13 pierre
- + added -Se in Makefile cvstest target
- + lexlevel cleanup
- normal_function_level main_program_level and unit_init_level defined
- * tins_cache grown to A_EMMS (gave range check error in asm readers)
- (test added in code !)
- * -Un option was wrong
- * _FAIL and _SELF only keyword inside
- constructors and methods respectively
- Revision 1.41 1998/11/12 11:19:40 pierre
- * fix for first line of function break
- Revision 1.40 1998/11/10 10:09:08 peter
- * va_list -> array of const
- Revision 1.39 1998/11/09 11:44:33 peter
- + va_list for printf support
- Revision 1.38 1998/10/21 15:12:49 pierre
- * bug fix for IOCHECK inside a procedure with iocheck modifier
- * removed the GPF for unexistant overloading
- (firstcall was called with procedinition=nil !)
- * changed typen to what Florian proposed
- gentypenode(p : pdef) sets the typenodetype field
- and resulttype is only set if inside bt_type block !
- Revision 1.37 1998/10/21 08:39:57 florian
- + ansistring operator +
- + $h and string[n] for n>255 added
- * small problem with TP fixed
- Revision 1.36 1998/10/20 08:06:39 pierre
- * several memory corruptions due to double freemem solved
- => never use p^.loc.location:=p^.left^.loc.location;
- + finally I added now by default
- that ra386dir translates global and unit symbols
- + added a first field in tsymtable and
- a nextsym field in tsym
- (this allows to obtain ordered type info for
- records and objects in gdb !)
- Revision 1.35 1998/10/16 08:51:45 peter
- + target_os.stackalignment
- + stack can be aligned at 2 or 4 byte boundaries
- Revision 1.34 1998/10/09 08:56:22 pierre
- * several memory leaks fixed
- Revision 1.33 1998/10/06 17:16:39 pierre
- * some memory leaks fixed (thanks to Peter for heaptrc !)
- Revision 1.32 1998/10/01 09:22:52 peter
- * fixed value openarray
- * ungettemp of arrayconstruct
- Revision 1.31 1998/09/28 16:57:15 pierre
- * changed all length(p^.value_str^) into str_length(p)
- to get it work with and without ansistrings
- * changed sourcefiles field of tmodule to a pointer
- Revision 1.30 1998/09/26 15:03:02 florian
- * small problems with DOM and excpetions fixed (code generation
- of raise was wrong and self was sometimes destroyed :()
- Revision 1.29 1998/09/25 00:04:00 florian
- * problems when calling class methods fixed
- Revision 1.28 1998/09/24 14:27:37 peter
- * some better support for openarray
- Revision 1.27 1998/09/24 09:02:13 peter
- * rewritten isconvertable to use case
- * array of .. and single variable are compatible
- Revision 1.26 1998/09/21 08:45:06 pierre
- + added vmt_offset in tobjectdef.write for fututre use
- (first steps to have objects without vmt if no virtual !!)
- + added fpu_used field for tabstractprocdef :
- sets this level to 2 if the functions return with value in FPU
- (is then set to correct value at parsing of implementation)
- THIS MIGHT refuse some code with FPU expression too complex
- that were accepted before and even in some cases
- that don't overflow in fact
- ( like if f : float; is a forward that finally in implementation
- only uses one fpu register !!)
- Nevertheless I think that it will improve security on
- FPU operations !!
- * most other changes only for UseBrowser code
- (added symtable references for record and objects)
- local switch for refs to args and local of each function
- (static symtable still missing)
- UseBrowser still not stable and probably broken by
- the definition hash array !!
- Revision 1.25 1998/09/20 12:26:35 peter
- * merged fixes
- Revision 1.24 1998/09/17 09:42:10 peter
- + pass_2 for cg386
- * Message() -> CGMessage() for pass_1/pass_2
- Revision 1.23 1998/09/14 10:43:45 peter
- * all internal RTL functions start with FPC_
- Revision 1.22.2.1 1998/09/20 12:20:06 peter
- * Fixed stack not on 4 byte boundary when doing a call
- Revision 1.22 1998/09/04 08:41:37 peter
- * updated some error CGMessages
- Revision 1.21 1998/09/01 12:47:57 peter
- * use pdef^.size instead of orddef^.typ
- Revision 1.20 1998/08/31 12:22:15 peter
- * secondinline moved to cg386inl
- Revision 1.19 1998/08/31 08:52:03 peter
- * fixed error 10 with succ() and pref()
- Revision 1.18 1998/08/20 21:36:38 peter
- * fixed 'with object do' bug
- Revision 1.17 1998/08/19 16:07:36 jonas
- * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
- Revision 1.16 1998/08/18 09:24:36 pierre
- * small warning position bug fixed
- * support_mmx switches splitting was missing
- * rhide error and warning output corrected
- Revision 1.15 1998/08/13 11:00:09 peter
- * fixed procedure<>procedure construct
- Revision 1.14 1998/08/11 14:05:33 peter
- * fixed sizeof(array of char)
- Revision 1.13 1998/08/10 14:49:45 peter
- + localswitches, moduleswitches, globalswitches splitting
- Revision 1.12 1998/07/30 13:30:31 florian
- * final implemenation of exception support, maybe it needs
- some fixes :)
- Revision 1.11 1998/07/24 22:16:52 florian
- * internal error 10 together with array access fixed. I hope
- that's the final fix.
- Revision 1.10 1998/07/18 22:54:23 florian
- * some ansi/wide/longstring support fixed:
- o parameter passing
- o returning as result from functions
- Revision 1.9 1998/07/07 17:40:37 peter
- * packrecords 4 works
- * word aligning of parameters
- Revision 1.8 1998/07/06 15:51:15 michael
- Added length checking for string reading
- Revision 1.7 1998/07/06 14:19:51 michael
- + Added calls for reading/writing ansistrings
- Revision 1.6 1998/07/01 15:28:48 peter
- + better writeln/readln handling, now 100% like tp7
- Revision 1.5 1998/06/25 14:04:17 peter
- + internal inc/dec
- Revision 1.4 1998/06/25 08:48:06 florian
- * first version of rtti support
- Revision 1.3 1998/06/09 16:01:33 pierre
- + added procedure directive parsing for procvars
- (accepted are popstack cdecl and pascal)
- + added C vars with the following syntax
- var C calias 'true_c_name';(can be followed by external)
- reason is that you must add the Cprefix
- which is target dependent
- Revision 1.2 1998/06/08 13:13:29 pierre
- + temporary variables now in temp_gen.pas unit
- because it is processor independent
- * mppc68k.bat modified to undefine i386 and support_mmx
- (which are defaults for i386)
- Revision 1.1 1998/06/05 17:44:10 peter
- * splitted cgi386
- }
|