ninl.pas 162 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  28. procedure ppuwrite(ppufile:tcompilerppufile);override;
  29. function dogetcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function simplify(forinline : boolean): tnode;override;
  33. function docompare(p: tnode): boolean; override;
  34. { pack and unpack are changed into for-loops by the compiler }
  35. function first_pack_unpack: tnode; virtual;
  36. protected
  37. { All the following routines currently
  38. call compilerprocs, unless they are
  39. overridden in which case, the code
  40. generator handles them.
  41. }
  42. function first_pi: tnode ; virtual;
  43. function first_arctan_real: tnode; virtual;
  44. function first_abs_real: tnode; virtual;
  45. function first_sqr_real: tnode; virtual;
  46. function first_sqrt_real: tnode; virtual;
  47. function first_ln_real: tnode; virtual;
  48. function first_cos_real: tnode; virtual;
  49. function first_sin_real: tnode; virtual;
  50. function first_exp_real: tnode; virtual;
  51. function first_frac_real: tnode; virtual;
  52. function first_round_real: tnode; virtual;
  53. function first_trunc_real: tnode; virtual;
  54. function first_int_real: tnode; virtual;
  55. function first_abs_long: tnode; virtual;
  56. function first_IncludeExclude: tnode; virtual;
  57. function first_get_frame: tnode; virtual;
  58. function first_setlength: tnode; virtual;
  59. function first_copy: tnode; virtual;
  60. { This one by default generates an internal error, because such
  61. nodes are not generated by the parser. It's however used internally
  62. by the JVM backend to create new dynamic arrays. }
  63. function first_new: tnode; virtual;
  64. function first_length: tnode; virtual;
  65. function first_box: tnode; virtual; abstract;
  66. function first_unbox: tnode; virtual; abstract;
  67. function first_assigned: tnode; virtual;
  68. function first_assert: tnode; virtual;
  69. function first_popcnt: tnode; virtual;
  70. { override these for Seg() support }
  71. function typecheck_seg: tnode; virtual;
  72. function first_seg: tnode; virtual;
  73. function first_sar: tnode; virtual;
  74. private
  75. function handle_str: tnode;
  76. function handle_reset_rewrite_typed: tnode;
  77. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  78. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  79. function handle_read_write: tnode;
  80. function handle_val: tnode;
  81. function handle_default: tnode;
  82. function handle_setlength: tnode;
  83. function handle_copy: tnode;
  84. function handle_box: tnode;
  85. function handle_unbox: tnode;
  86. end;
  87. tinlinenodeclass = class of tinlinenode;
  88. var
  89. cinlinenode : tinlinenodeclass = tinlinenode;
  90. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  91. implementation
  92. uses
  93. verbose,globals,systems,constexp,
  94. globtype,cutils,fmodule,
  95. symconst,symdef,symsym,symtable,paramgr,defutil,symbase,
  96. pass_1,
  97. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  98. nobjc,objcdef,
  99. cgbase,procinfo
  100. ;
  101. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  102. begin
  103. geninlinenode:=cinlinenode.create(number,is_const,l);
  104. end;
  105. {*****************************************************************************
  106. TINLINENODE
  107. *****************************************************************************}
  108. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  109. begin
  110. inherited create(inlinen,l);
  111. if is_const then
  112. include(flags,nf_inlineconst);
  113. inlinenumber:=number;
  114. end;
  115. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  116. begin
  117. inherited ppuload(t,ppufile);
  118. inlinenumber:=ppufile.getbyte;
  119. end;
  120. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  121. begin
  122. inherited ppuwrite(ppufile);
  123. ppufile.putbyte(inlinenumber);
  124. end;
  125. function tinlinenode.dogetcopy : tnode;
  126. var
  127. n : tinlinenode;
  128. begin
  129. n:=tinlinenode(inherited dogetcopy);
  130. n.inlinenumber:=inlinenumber;
  131. result:=n;
  132. end;
  133. function get_str_int_func(def: tdef): string;
  134. var
  135. ordtype: tordtype;
  136. begin
  137. ordtype := torddef(def).ordtype;
  138. if not (ordtype in [scurrency,s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  139. internalerror(2013032603);
  140. if is_oversizedord(def) then
  141. begin
  142. case ordtype of
  143. scurrency,
  144. s64bit: exit('int64');
  145. u64bit: exit('qword');
  146. s32bit: exit('longint');
  147. u32bit: exit('longword');
  148. s16bit: exit('smallint');
  149. u16bit: exit('word');
  150. else
  151. internalerror(2013032604);
  152. end;
  153. end
  154. else
  155. begin
  156. if is_nativeuint(def) then
  157. exit('uint')
  158. else
  159. exit('sint');
  160. end;
  161. internalerror(2013032605);
  162. end;
  163. function tinlinenode.handle_str : tnode;
  164. var
  165. lenpara,
  166. fracpara,
  167. newparas,
  168. tmppara,
  169. dest,
  170. source : tcallparanode;
  171. procname: string;
  172. is_real,is_enum : boolean;
  173. rt : aint;
  174. begin
  175. result := cerrornode.create;
  176. { get destination string }
  177. dest := tcallparanode(left);
  178. { get source para (number) }
  179. source := dest;
  180. while assigned(source.right) do
  181. source := tcallparanode(source.right);
  182. { destination parameter must be a normal (not a colon) parameter, this
  183. check is needed because str(v:len) also has 2 parameters }
  184. if (source=dest) or
  185. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  186. begin
  187. CGMessage1(parser_e_wrong_parameter_size,'Str');
  188. exit;
  189. end;
  190. { in case we are in a generic definition, we cannot
  191. do all checks, the parameters might be type parameters }
  192. if df_generic in current_procinfo.procdef.defoptions then
  193. begin
  194. result.Free;
  195. result:=nil;
  196. resultdef:=voidtype;
  197. exit;
  198. end;
  199. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  200. is_enum:=source.left.resultdef.typ=enumdef;
  201. if ((dest.left.resultdef.typ<>stringdef) and
  202. not(is_chararray(dest.left.resultdef))) or
  203. not(is_real or is_enum or
  204. (source.left.resultdef.typ=orddef)) then
  205. begin
  206. CGMessagePos(fileinfo,parser_e_illegal_expression);
  207. exit;
  208. end;
  209. { get len/frac parameters }
  210. lenpara := nil;
  211. fracpara := nil;
  212. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  213. begin
  214. lenpara := tcallparanode(dest.right);
  215. { we can let the callnode do the type checking of these parameters too, }
  216. { but then the error messages aren't as nice }
  217. if not is_integer(lenpara.resultdef) then
  218. begin
  219. CGMessagePos1(lenpara.fileinfo,
  220. type_e_integer_expr_expected,lenpara.resultdef.typename);
  221. exit;
  222. end;
  223. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  224. begin
  225. { parameters are in reverse order! }
  226. fracpara := lenpara;
  227. lenpara := tcallparanode(lenpara.right);
  228. if not is_real then
  229. begin
  230. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  231. exit
  232. end;
  233. if not is_integer(lenpara.resultdef) then
  234. begin
  235. CGMessagePos1(lenpara.fileinfo,
  236. type_e_integer_expr_expected,lenpara.resultdef.typename);
  237. exit;
  238. end;
  239. end;
  240. end;
  241. { generate the parameter list for the compilerproc }
  242. newparas := dest;
  243. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  244. if is_real then
  245. begin
  246. { insert realtype parameter }
  247. if not is_currency(source.resultdef) then
  248. begin
  249. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  250. newparas.right := ccallparanode.create(cordconstnode.create(
  251. rt,s32inttype,true),newparas.right);
  252. tmppara:=tcallparanode(newparas.right);
  253. end
  254. else
  255. tmppara:=newparas;
  256. { if necessary, insert a fraction parameter }
  257. if not assigned(fracpara) then
  258. begin
  259. tmppara.right := ccallparanode.create(
  260. cordconstnode.create(int64(-1),s32inttype,false),
  261. tmppara.right);
  262. fracpara := tcallparanode(tmppara.right);
  263. end;
  264. { if necessary, insert a length para }
  265. if not assigned(lenpara) then
  266. fracpara.right := ccallparanode.create(
  267. cordconstnode.create(int64(-32767),s32inttype,false),
  268. fracpara.right);
  269. end
  270. else if is_enum then
  271. begin
  272. {Insert a reference to the ord2string index.}
  273. newparas.right:=Ccallparanode.create(
  274. Caddrnode.create_internal(
  275. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  276. ),
  277. newparas.right);
  278. {Insert a reference to the typinfo.}
  279. newparas.right:=Ccallparanode.create(
  280. Caddrnode.create_internal(
  281. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  282. ),
  283. newparas.right);
  284. {Insert a type conversion from the enumeration to longint.}
  285. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  286. typecheckpass(source.left);
  287. { if necessary, insert a length para }
  288. if not assigned(lenpara) then
  289. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  290. Ccallparanode.create(
  291. cordconstnode.create(int64(-1),s32inttype,false),
  292. Tcallparanode(Tcallparanode(newparas.right).right).right
  293. );
  294. end
  295. else
  296. { for a normal parameter, insert a only length parameter if one is missing }
  297. if not assigned(lenpara) then
  298. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  299. newparas.right);
  300. { remove the parameters from the original node so they won't get disposed, }
  301. { since they're reused }
  302. left := nil;
  303. { create procedure name }
  304. if is_chararray(dest.resultdef) then
  305. procname:='fpc_chararray_'
  306. else
  307. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  308. if is_real then
  309. if is_currency(source.resultdef) then
  310. procname := procname + 'currency'
  311. else
  312. procname := procname + 'float'
  313. else if is_enum then
  314. procname:=procname+'enum'
  315. else
  316. case torddef(source.resultdef).ordtype of
  317. pasbool8,pasbool16,pasbool32,pasbool64,
  318. bool8bit,bool16bit,bool32bit,bool64bit:
  319. procname := procname + 'bool';
  320. else
  321. procname := procname + get_str_int_func(source.resultdef);
  322. end;
  323. { for ansistrings insert the encoding argument }
  324. if is_ansistring(dest.resultdef) then
  325. newparas:=ccallparanode.create(cordconstnode.create(
  326. getparaencoding(dest.resultdef),u16inttype,true),newparas);
  327. { free the errornode we generated in the beginning }
  328. result.free;
  329. { create the call node, }
  330. result := ccallnode.createintern(procname,newparas);
  331. end;
  332. function tinlinenode.handle_default: tnode;
  333. function getdefaultvarsym(def:tdef):tnode;
  334. var
  335. hashedid : thashedidstring;
  336. srsym : tsym;
  337. srsymtable : tsymtable;
  338. defaultname : tidstring;
  339. begin
  340. if not assigned(def) or
  341. not (def.typ in [arraydef,recorddef,variantdef,objectdef,procvardef]) or
  342. ((def.typ=objectdef) and not is_object(def)) then
  343. internalerror(201202101);
  344. defaultname:=make_mangledname('zero',def.owner,def.typesym.Name);
  345. hashedid.id:=defaultname;
  346. { the default sym is always part of the current procedure/function }
  347. srsymtable:=current_procinfo.procdef.localst;
  348. srsym:=tsym(srsymtable.findwithhash(hashedid));
  349. if not assigned(srsym) then
  350. begin
  351. { no valid default variable found, so create it }
  352. srsym:=tlocalvarsym.create(defaultname,vs_const,def,[]);
  353. srsymtable.insert(srsym);
  354. { mark the staticvarsym as typedconst }
  355. include(tabstractvarsym(srsym).varoptions,vo_is_typed_const);
  356. include(tabstractvarsym(srsym).varoptions,vo_is_default_var);
  357. { The variable has a value assigned }
  358. tabstractvarsym(srsym).varstate:=vs_initialised;
  359. { the variable can't be placed in a register }
  360. tabstractvarsym(srsym).varregable:=vr_none;
  361. end;
  362. result:=cloadnode.create(srsym,srsymtable);
  363. end;
  364. var
  365. def : tdef;
  366. begin
  367. if not assigned(left) or (left.nodetype<>typen) then
  368. internalerror(2012032101);
  369. def:=ttypenode(left).typedef;
  370. result:=nil;
  371. case def.typ of
  372. enumdef,
  373. orddef:
  374. { don't do a rangecheck as Default will also return 0
  375. for the following types (Delphi compatible):
  376. TRange1 = -10..-5;
  377. TRange2 = 5..10;
  378. TEnum = (a:=5;b:=10); }
  379. result:=cordconstnode.create(0,def,false);
  380. classrefdef,
  381. pointerdef:
  382. result:=cpointerconstnode.create(0,def);
  383. procvardef:
  384. if tprocvardef(def).size<>sizeof(pint) then
  385. result:=getdefaultvarsym(def)
  386. else
  387. result:=cpointerconstnode.create(0,def);
  388. stringdef:
  389. result:=cstringconstnode.createstr('');
  390. floatdef:
  391. result:=crealconstnode.create(0,def);
  392. objectdef:
  393. begin
  394. if is_implicit_pointer_object_type(def) then
  395. result:=cpointerconstnode.create(0,def)
  396. else
  397. if is_object(def) then
  398. begin
  399. { Delphi does not recursively check whether
  400. an object contains unsupported types }
  401. if not (m_delphi in current_settings.modeswitches) and
  402. not is_valid_for_default(def) then
  403. Message(type_e_type_not_allowed_for_default);
  404. result:=getdefaultvarsym(def);
  405. end
  406. else
  407. Message(type_e_type_not_allowed_for_default);
  408. end;
  409. variantdef,
  410. recorddef:
  411. begin
  412. { Delphi does not recursively check whether a record
  413. contains unsupported types }
  414. if (def.typ=recorddef) and not (m_delphi in current_settings.modeswitches) and
  415. not is_valid_for_default(def) then
  416. Message(type_e_type_not_allowed_for_default);
  417. result:=getdefaultvarsym(def);
  418. end;
  419. setdef:
  420. begin
  421. result:=csetconstnode.create(nil,def);
  422. New(tsetconstnode(result).value_set);
  423. tsetconstnode(result).value_set^:=[];
  424. end;
  425. arraydef:
  426. begin
  427. { can other array types be parsed by single_type? }
  428. if ado_isdynamicarray in tarraydef(def).arrayoptions then
  429. result:=cpointerconstnode.create(0,def)
  430. else
  431. begin
  432. result:=getdefaultvarsym(def);
  433. end;
  434. end;
  435. undefineddef:
  436. begin
  437. if sp_generic_dummy in def.typesym.symoptions then
  438. begin
  439. { this matches the error messages that are printed
  440. in case of non-Delphi modes }
  441. Message(parser_e_no_generics_as_types);
  442. Message(type_e_type_id_expected);
  443. end
  444. else
  445. result:=cpointerconstnode.create(0,def);
  446. end;
  447. else
  448. Message(type_e_type_not_allowed_for_default);
  449. end;
  450. if not assigned(result) then
  451. result:=cerrornode.create;
  452. end;
  453. function tinlinenode.handle_reset_rewrite_typed: tnode;
  454. begin
  455. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  456. { a typed file as argument and we don't have to check it again (JM) }
  457. { add the recsize parameter }
  458. { note: for some reason, the parameter of intern procedures with only one }
  459. { parameter is gets lifted out of its original tcallparanode (see round }
  460. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  461. left := ccallparanode.create(cordconstnode.create(
  462. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  463. ccallparanode.create(left,nil));
  464. { create the correct call }
  465. if m_iso in current_settings.modeswitches then
  466. begin
  467. if inlinenumber=in_reset_typedfile then
  468. result := ccallnode.createintern('fpc_reset_typed_iso',left)
  469. else
  470. result := ccallnode.createintern('fpc_rewrite_typed_iso',left);
  471. end
  472. else
  473. begin
  474. if inlinenumber=in_reset_typedfile then
  475. result := ccallnode.createintern('fpc_reset_typed',left)
  476. else
  477. result := ccallnode.createintern('fpc_rewrite_typed',left);
  478. end;
  479. { make sure left doesn't get disposed, since we use it in the new call }
  480. left := nil;
  481. end;
  482. procedure maybe_convert_to_string(var n: tnode);
  483. begin
  484. { stringconstnodes are arrays of char. It's much more }
  485. { efficient to write a constant string, so convert }
  486. { either to shortstring or ansistring depending on }
  487. { length }
  488. if (n.nodetype=stringconstn) then
  489. if is_chararray(n.resultdef) then
  490. if (tstringconstnode(n).len<=255) then
  491. inserttypeconv(n,cshortstringtype)
  492. else
  493. inserttypeconv(n,getansistringdef)
  494. else if is_widechararray(n.resultdef) then
  495. inserttypeconv(n,cunicodestringtype);
  496. end;
  497. procedure get_read_write_int_func(def: tdef; out func_suffix: string; out readfunctype: tdef);
  498. var
  499. ordtype: tordtype;
  500. begin
  501. ordtype := torddef(def).ordtype;
  502. if not (ordtype in [s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  503. internalerror(2013032601);
  504. if is_oversizedint(def) then
  505. begin
  506. case ordtype of
  507. s64bit:
  508. begin
  509. func_suffix := 'int64';
  510. readfunctype:=s64inttype;
  511. end;
  512. u64bit :
  513. begin
  514. func_suffix := 'qword';
  515. readfunctype:=u64inttype;
  516. end;
  517. s32bit:
  518. begin
  519. func_suffix := 'longint';
  520. readfunctype:=s32inttype;
  521. end;
  522. u32bit :
  523. begin
  524. func_suffix := 'longword';
  525. readfunctype:=u32inttype;
  526. end;
  527. s16bit:
  528. begin
  529. func_suffix := 'smallint';
  530. readfunctype:=s16inttype;
  531. end;
  532. u16bit :
  533. begin
  534. func_suffix := 'word';
  535. readfunctype:=u16inttype;
  536. end;
  537. else
  538. internalerror(2013032602);
  539. end;
  540. end
  541. else
  542. begin
  543. case ordtype of
  544. s64bit,
  545. s32bit,
  546. s16bit,
  547. s8bit:
  548. begin
  549. func_suffix := 'sint';
  550. readfunctype := sinttype;
  551. end;
  552. u64bit,
  553. u32bit,
  554. u16bit,
  555. u8bit:
  556. begin
  557. func_suffix := 'uint';
  558. readfunctype := uinttype;
  559. end;
  560. end;
  561. end;
  562. end;
  563. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  564. {Read(ln)/write(ln) for text files.}
  565. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  566. var error_para,is_real,special_handling,found_error,do_read:boolean;
  567. p1:Tnode;
  568. nextpara,
  569. indexpara,
  570. lenpara,
  571. para,
  572. fracpara:Tcallparanode;
  573. temp:Ttempcreatenode;
  574. readfunctype:Tdef;
  575. name:string[63];
  576. func_suffix:string[8];
  577. begin
  578. para:=Tcallparanode(params);
  579. found_error:=false;
  580. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  581. name:='';
  582. while assigned(para) do
  583. begin
  584. { is this parameter faulty? }
  585. error_para:=false;
  586. { is this parameter a real? }
  587. is_real:=false;
  588. { type used for the read(), this is used to check
  589. whether a temp is needed for range checking }
  590. readfunctype:=nil;
  591. { can't read/write types }
  592. if (para.left.nodetype=typen) and not(ttypenode(para.left).typedef.typ=undefineddef) then
  593. begin
  594. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  595. error_para := true;
  596. end;
  597. { support writeln(procvar) }
  598. if para.left.resultdef.typ=procvardef then
  599. begin
  600. p1:=ccallnode.create_procvar(nil,para.left);
  601. typecheckpass(p1);
  602. para.left:=p1;
  603. end;
  604. if inlinenumber in [in_write_x,in_writeln_x] then
  605. { prefer strings to chararrays }
  606. maybe_convert_to_string(para.left);
  607. case para.left.resultdef.typ of
  608. stringdef :
  609. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  610. pointerdef :
  611. begin
  612. if (not is_pchar(para.left.resultdef)) or do_read then
  613. begin
  614. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  615. error_para := true;
  616. end
  617. else
  618. name:=procprefixes[do_read]+'pchar_as_pointer';
  619. end;
  620. floatdef :
  621. begin
  622. is_real:=true;
  623. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  624. name := procprefixes[do_read]+'currency'
  625. else
  626. begin
  627. name := procprefixes[do_read]+'float';
  628. readfunctype:=pbestrealtype^;
  629. end;
  630. { iso pascal needs a different handler }
  631. if (m_iso in current_settings.modeswitches) and do_read then
  632. name:=name+'_iso';
  633. end;
  634. enumdef:
  635. begin
  636. name:=procprefixes[do_read]+'enum';
  637. readfunctype:=s32inttype;
  638. end;
  639. orddef :
  640. begin
  641. case Torddef(para.left.resultdef).ordtype of
  642. s8bit,
  643. s16bit,
  644. s32bit,
  645. s64bit,
  646. u8bit,
  647. u16bit,
  648. u32bit,
  649. u64bit:
  650. begin
  651. get_read_write_int_func(para.left.resultdef,func_suffix,readfunctype);
  652. name := procprefixes[do_read]+func_suffix;
  653. if (m_iso in current_settings.modeswitches) and do_read then
  654. name:=name+'_iso';
  655. end;
  656. uchar :
  657. begin
  658. name := procprefixes[do_read]+'char';
  659. { iso pascal needs a different handler }
  660. if (m_iso in current_settings.modeswitches) and do_read then
  661. name:=name+'_iso';
  662. readfunctype:=cansichartype;
  663. end;
  664. uwidechar :
  665. begin
  666. name := procprefixes[do_read]+'widechar';
  667. readfunctype:=cwidechartype;
  668. end;
  669. scurrency:
  670. begin
  671. name := procprefixes[do_read]+'currency';
  672. { iso pascal needs a different handler }
  673. if (m_iso in current_settings.modeswitches) and do_read then
  674. name:=name+'_iso';
  675. readfunctype:=s64currencytype;
  676. is_real:=true;
  677. end;
  678. pasbool8,
  679. pasbool16,
  680. pasbool32,
  681. pasbool64,
  682. bool8bit,
  683. bool16bit,
  684. bool32bit,
  685. bool64bit:
  686. if do_read then
  687. begin
  688. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  689. error_para := true;
  690. end
  691. else
  692. begin
  693. name := procprefixes[do_read]+'boolean';
  694. readfunctype:=pasbool8type;
  695. end
  696. else
  697. begin
  698. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  699. error_para := true;
  700. end;
  701. end;
  702. end;
  703. variantdef :
  704. name:=procprefixes[do_read]+'variant';
  705. arraydef :
  706. begin
  707. if is_chararray(para.left.resultdef) then
  708. name := procprefixes[do_read]+'pchar_as_array'
  709. else
  710. begin
  711. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  712. error_para := true;
  713. end
  714. end;
  715. { generic parameter }
  716. undefineddef:
  717. { don't try to generate any code for a writeln on a generic parameter }
  718. error_para:=true;
  719. else
  720. begin
  721. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  722. error_para := true;
  723. end;
  724. end;
  725. { iso pascal needs a different handler }
  726. if (m_iso in current_settings.modeswitches) and not(do_read) then
  727. name:=name+'_iso';
  728. { check for length/fractional colon para's }
  729. fracpara:=nil;
  730. lenpara:=nil;
  731. indexpara:=nil;
  732. if assigned(para.right) and
  733. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  734. begin
  735. lenpara := tcallparanode(para.right);
  736. if assigned(lenpara.right) and
  737. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  738. fracpara:=tcallparanode(lenpara.right);
  739. end;
  740. { get the next parameter now already, because we're going }
  741. { to muck around with the pointers }
  742. if assigned(fracpara) then
  743. nextpara := tcallparanode(fracpara.right)
  744. else if assigned(lenpara) then
  745. nextpara := tcallparanode(lenpara.right)
  746. else
  747. nextpara := tcallparanode(para.right);
  748. { check if a fracpara is allowed }
  749. if assigned(fracpara) and not is_real then
  750. begin
  751. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  752. error_para := true;
  753. end
  754. else if assigned(lenpara) and do_read then
  755. begin
  756. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  757. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  758. error_para := true;
  759. end;
  760. { adjust found_error }
  761. found_error := found_error or error_para;
  762. if not error_para then
  763. begin
  764. special_handling:=false;
  765. { create dummy frac/len para's if necessary }
  766. if not do_read then
  767. begin
  768. { difference in default value for floats and the rest :( }
  769. if not is_real then
  770. begin
  771. if not assigned(lenpara) then
  772. begin
  773. if m_iso in current_settings.modeswitches then
  774. lenpara := ccallparanode.create(
  775. cordconstnode.create(-1,s32inttype,false),nil)
  776. else
  777. lenpara := ccallparanode.create(
  778. cordconstnode.create(0,s32inttype,false),nil);
  779. end
  780. else
  781. { make sure we don't pass the successive }
  782. { parameters too. We also already have a }
  783. { reference to the next parameter in }
  784. { nextpara }
  785. lenpara.right := nil;
  786. end
  787. else
  788. begin
  789. if not assigned(lenpara) then
  790. lenpara := ccallparanode.create(
  791. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  792. { also create a default fracpara if necessary }
  793. if not assigned(fracpara) then
  794. fracpara := ccallparanode.create(
  795. cordconstnode.create(int64(-1),s32inttype,false),nil);
  796. { add it to the lenpara }
  797. lenpara.right := fracpara;
  798. if not is_currency(para.left.resultdef) then
  799. begin
  800. { and add the realtype para (this also removes the link }
  801. { to any parameters coming after it) }
  802. fracpara.right := ccallparanode.create(
  803. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  804. s32inttype,true),nil);
  805. end
  806. else
  807. fracpara.right:=nil;
  808. end;
  809. if para.left.resultdef.typ=enumdef then
  810. begin
  811. {To write(ln) an enum we need a some extra parameters.}
  812. {Insert a reference to the ord2string index.}
  813. indexpara:=Ccallparanode.create(
  814. Caddrnode.create_internal(
  815. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  816. ),
  817. nil);
  818. {Insert a reference to the typinfo.}
  819. indexpara:=Ccallparanode.create(
  820. Caddrnode.create_internal(
  821. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  822. ),
  823. indexpara);
  824. {Insert a type conversion to to convert the enum to longint.}
  825. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  826. typecheckpass(para.left);
  827. end;
  828. end
  829. else
  830. begin
  831. {To read(ln) an enum we need a an extra parameter.}
  832. if para.left.resultdef.typ=enumdef then
  833. begin
  834. {Insert a reference to the string2ord index.}
  835. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  836. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  837. ),nil);
  838. {Insert a type conversion to to convert the enum to longint.}
  839. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  840. typecheckpass(para.left);
  841. end;
  842. { special handling of reading small numbers, because the helpers }
  843. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  844. { use functions because then the call to FPC_IOCHECK destroys }
  845. { their result before we can store it }
  846. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  847. special_handling:=true;
  848. end;
  849. if special_handling then
  850. begin
  851. { since we're not going to pass the parameter as var-parameter }
  852. { to the read function, manually check whether the parameter }
  853. { can be used as var-parameter (e.g., whether it isn't a }
  854. { property) }
  855. valid_for_var(para.left,true);
  856. { create the parameter list: the temp ... }
  857. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  858. addstatement(Tstatementnode(newstatement),temp);
  859. { ... and the file }
  860. p1 := ccallparanode.create(ctemprefnode.create(temp),
  861. filepara.getcopy);
  862. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  863. { create the call to the helper }
  864. addstatement(Tstatementnode(newstatement),
  865. ccallnode.createintern(name,tcallparanode(p1)));
  866. { assign the result to the original var (this automatically }
  867. { takes care of range checking) }
  868. addstatement(Tstatementnode(newstatement),
  869. cassignmentnode.create(para.left,
  870. ctemprefnode.create(temp)));
  871. { release the temp location }
  872. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  873. { statement of para is used }
  874. para.left := nil;
  875. { free the enclosing tcallparanode, but not the }
  876. { parameters coming after it }
  877. para.right := nil;
  878. para.free;
  879. end
  880. else
  881. { read of non s/u-8/16bit, or a write }
  882. begin
  883. { add the filepara to the current parameter }
  884. para.right := filepara.getcopy;
  885. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  886. already linked with the lenpara if necessary).}
  887. if indexpara=nil then
  888. Tcallparanode(para.right).right:=lenpara
  889. else
  890. begin
  891. if lenpara=nil then
  892. Tcallparanode(para.right).right:=indexpara
  893. else
  894. begin
  895. Tcallparanode(para.right).right:=lenpara;
  896. lenpara.right:=indexpara;
  897. end;
  898. { indexpara.right:=lenpara;}
  899. end;
  900. { in case of writing a chararray, add whether it's zero-based }
  901. if para.left.resultdef.typ=arraydef then
  902. para := ccallparanode.create(cordconstnode.create(
  903. ord(tarraydef(para.left.resultdef).lowrange=0),pasbool8type,false),para)
  904. else
  905. { in case of reading an ansistring pass a codepage argument }
  906. if do_read and is_ansistring(para.left.resultdef) then
  907. para:=ccallparanode.create(cordconstnode.create(
  908. getparaencoding(para.left.resultdef),u16inttype,true),para);
  909. { create the call statement }
  910. addstatement(Tstatementnode(newstatement),
  911. ccallnode.createintern(name,para));
  912. end
  913. end
  914. else
  915. { error_para = true }
  916. begin
  917. { free the parameter, since it isn't referenced anywhere anymore }
  918. para.right := nil;
  919. para.free;
  920. if assigned(lenpara) then
  921. begin
  922. lenpara.right := nil;
  923. lenpara.free;
  924. end;
  925. if assigned(fracpara) then
  926. begin
  927. fracpara.right := nil;
  928. fracpara.free;
  929. end;
  930. end;
  931. { process next parameter }
  932. para := nextpara;
  933. end;
  934. { if no error, add the write(ln)/read(ln) end calls }
  935. if not found_error then
  936. begin
  937. case inlinenumber of
  938. in_read_x,
  939. in_readstr_x:
  940. name:='fpc_read_end';
  941. in_write_x,
  942. in_writestr_x:
  943. name:='fpc_write_end';
  944. in_readln_x:
  945. begin
  946. name:='fpc_readln_end';
  947. if m_iso in current_settings.modeswitches then
  948. name:=name+'_iso';
  949. end;
  950. in_writeln_x:
  951. name:='fpc_writeln_end';
  952. end;
  953. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara));
  954. end;
  955. handle_text_read_write:=found_error;
  956. end;
  957. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  958. {Read/write for typed files.}
  959. const procprefixes:array[boolean,boolean] of string[19]=(('fpc_typed_write','fpc_typed_read'),
  960. ('fpc_typed_write','fpc_typed_read_iso'));
  961. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  962. var found_error,do_read,is_rwstr:boolean;
  963. para,nextpara:Tcallparanode;
  964. p1:Tnode;
  965. temp:Ttempcreatenode;
  966. begin
  967. found_error:=false;
  968. para:=Tcallparanode(params);
  969. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  970. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  971. temp:=nil;
  972. { add the typesize to the filepara }
  973. if filepara.resultdef.typ=filedef then
  974. filepara.right := ccallparanode.create(cordconstnode.create(
  975. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  976. { check for "no parameters" (you need at least one extra para for typed files) }
  977. if not assigned(para) then
  978. begin
  979. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  980. found_error := true;
  981. end;
  982. { process all parameters }
  983. while assigned(para) do
  984. begin
  985. { check if valid parameter }
  986. if para.left.nodetype=typen then
  987. begin
  988. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  989. found_error := true;
  990. end;
  991. { support writeln(procvar) }
  992. if (para.left.resultdef.typ=procvardef) then
  993. begin
  994. p1:=ccallnode.create_procvar(nil,para.left);
  995. typecheckpass(p1);
  996. para.left:=p1;
  997. end;
  998. if filepara.resultdef.typ=filedef then
  999. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  1000. if assigned(para.right) and
  1001. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  1002. begin
  1003. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  1004. { skip all colon para's }
  1005. nextpara := tcallparanode(tcallparanode(para.right).right);
  1006. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  1007. nextpara := tcallparanode(nextpara.right);
  1008. found_error := true;
  1009. end
  1010. else
  1011. { get next parameter }
  1012. nextpara := tcallparanode(para.right);
  1013. { When we have a call, we have a problem: you can't pass the }
  1014. { result of a call as a formal const parameter. Solution: }
  1015. { assign the result to a temp and pass this temp as parameter }
  1016. { This is not very efficient, but write(typedfile,x) is }
  1017. { already slow by itself anyway (no buffering) (JM) }
  1018. { Actually, thge same goes for every non-simple expression }
  1019. { (such as an addition, ...) -> put everything but load nodes }
  1020. { into temps (JM) }
  1021. { of course, this must only be allowed for writes!!! (JM) }
  1022. if not(do_read) and (para.left.nodetype <> loadn) then
  1023. begin
  1024. { create temp for result }
  1025. temp := ctempcreatenode.create(para.left.resultdef,
  1026. para.left.resultdef.size,tt_persistent,false);
  1027. addstatement(Tstatementnode(newstatement),temp);
  1028. { assign result to temp }
  1029. addstatement(Tstatementnode(newstatement),
  1030. cassignmentnode.create(ctemprefnode.create(temp),
  1031. para.left));
  1032. { replace (reused) paranode with temp }
  1033. para.left := ctemprefnode.create(temp);
  1034. end;
  1035. { add fileparameter }
  1036. para.right := filepara.getcopy;
  1037. { create call statment }
  1038. { since the parameters are in the correct order, we have to insert }
  1039. { the statements always at the end of the current block }
  1040. addstatement(Tstatementnode(newstatement),
  1041. Ccallnode.createintern(procprefixes[m_iso in current_settings.modeswitches,do_read],para
  1042. ));
  1043. { if we used a temp, free it }
  1044. if para.left.nodetype = temprefn then
  1045. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1046. { process next parameter }
  1047. para := nextpara;
  1048. end;
  1049. { free the file parameter }
  1050. filepara.free;
  1051. handle_typed_read_write:=found_error;
  1052. end;
  1053. function tinlinenode.handle_read_write: tnode;
  1054. var
  1055. filepara,
  1056. nextpara,
  1057. params : tcallparanode;
  1058. newstatement : tstatementnode;
  1059. newblock : tblocknode;
  1060. filetemp : Ttempcreatenode;
  1061. name : string[31];
  1062. textsym : ttypesym;
  1063. is_typed,
  1064. do_read,
  1065. is_rwstr,
  1066. found_error : boolean;
  1067. begin
  1068. filepara := nil;
  1069. is_typed := false;
  1070. filetemp := nil;
  1071. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1072. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1073. { if we fail, we can quickly exit this way. We must generate something }
  1074. { instead of the inline node, because firstpass will bomb with an }
  1075. { internalerror if it encounters a read/write }
  1076. result := cerrornode.create;
  1077. { reverse the parameters (needed to get the colon parameters in the }
  1078. { correct order when processing write(ln) }
  1079. left := reverseparameters(tcallparanode(left));
  1080. if is_rwstr then
  1081. begin
  1082. filepara := tcallparanode(left);
  1083. { needs at least two parameters: source/dest string + min. 1 value }
  1084. if not(assigned(filepara)) or
  1085. not(assigned(filepara.right)) then
  1086. begin
  1087. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  1088. exit;
  1089. end
  1090. else if (filepara.resultdef.typ <> stringdef) then
  1091. begin
  1092. { convert chararray to string, or give an appropriate error message }
  1093. { (if you want to optimize to use shortstring, keep in mind that }
  1094. { readstr internally always uses ansistring, and to account for }
  1095. { chararrays with > 255 characters) }
  1096. inserttypeconv(filepara.left,getansistringdef);
  1097. filepara.resultdef:=filepara.left.resultdef;
  1098. if codegenerror then
  1099. exit;
  1100. end
  1101. end
  1102. else if assigned(left) then
  1103. begin
  1104. { check if we have a file parameter and if yes, what kind it is }
  1105. filepara := tcallparanode(left);
  1106. if (filepara.resultdef.typ=filedef) then
  1107. begin
  1108. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  1109. begin
  1110. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  1111. exit;
  1112. end
  1113. else
  1114. begin
  1115. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  1116. begin
  1117. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  1118. begin
  1119. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  1120. exit;
  1121. end;
  1122. is_typed := true;
  1123. end
  1124. end;
  1125. end
  1126. else
  1127. filepara := nil;
  1128. end;
  1129. { create a blocknode in which the successive write/read statements will be }
  1130. { put, since they belong together. Also create a dummy statement already to }
  1131. { make inserting of additional statements easier }
  1132. newblock:=internalstatements(newstatement);
  1133. if is_rwstr then
  1134. begin
  1135. { create a dummy temp text file that will be used to cache the
  1136. readstr/writestr state. Can't use a global variable in the system
  1137. unit because these can be nested (in case of parameters to
  1138. writestr that are function calls to functions that also call
  1139. readstr/writestr) }
  1140. textsym:=search_system_type('TEXT');
  1141. filetemp:=ctempcreatenode.create(textsym.typedef,textsym.typedef.size,tt_persistent,false);
  1142. addstatement(newstatement,filetemp);
  1143. if (do_read) then
  1144. name:='fpc_setupreadstr_'
  1145. else
  1146. name:='fpc_setupwritestr_';
  1147. name:=name+tstringdef(filepara.resultdef).stringtypname;
  1148. { the file para is a var parameter, but it is properly initialized,
  1149. so it should be actually an out parameter }
  1150. if not(do_read) then
  1151. set_varstate(filepara.left,vs_written,[]);
  1152. { remove the source/destination string parameter from the }
  1153. { parameter chain }
  1154. left:=filepara.right;
  1155. filepara.right:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1156. { in case of a writestr() to an ansistring, also pass the string's
  1157. code page }
  1158. if not do_read and
  1159. is_ansistring(filepara.left.resultdef) then
  1160. filepara:=ccallparanode.create(genintconstnode(tstringdef(filepara.left.resultdef).encoding),filepara);
  1161. { pass the temp text file and the source/destination string to the
  1162. setup routine, which will store the string's address in the
  1163. textrec }
  1164. addstatement(newstatement,ccallnode.createintern(name,filepara));
  1165. filepara:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1166. end
  1167. { if we don't have a filepara, create one containing the default }
  1168. else if not assigned(filepara) then
  1169. begin
  1170. { since the input/output variables are threadvars loading them into
  1171. a temp once is faster. Create a temp which will hold a pointer to the file }
  1172. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1173. addstatement(newstatement,filetemp);
  1174. { make sure the resultdef of the temp (and as such of the }
  1175. { temprefs coming after it) is set (necessary because the }
  1176. { temprefs will be part of the filepara, of which we need }
  1177. { the resultdef later on and temprefs can only be }
  1178. { typecheckpassed if the resultdef of the temp is known) }
  1179. typecheckpass(tnode(filetemp));
  1180. { assign the address of the file to the temp }
  1181. if do_read then
  1182. name := 'input'
  1183. else
  1184. name := 'output';
  1185. addstatement(newstatement,
  1186. cassignmentnode.create(ctemprefnode.create(filetemp),
  1187. ccallnode.createintern('fpc_get_'+name,nil)));
  1188. { create a new fileparameter as follows: file_type(temp^) }
  1189. { (so that we pass the value and not the address of the temp }
  1190. { to the read/write routine) }
  1191. textsym:=search_system_type('TEXT');
  1192. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  1193. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  1194. end
  1195. else
  1196. { remove filepara from the parameter chain }
  1197. begin
  1198. left := filepara.right;
  1199. filepara.right := nil;
  1200. { the file para is a var parameter, but it must be valid already }
  1201. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  1202. { check if we should make a temp to store the result of a complex }
  1203. { expression (better heuristics, anyone?) (JM) }
  1204. if (filepara.left.nodetype <> loadn) then
  1205. begin
  1206. { create a temp which will hold a pointer to the file }
  1207. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1208. { add it to the statements }
  1209. addstatement(newstatement,filetemp);
  1210. { make sure the resultdef of the temp (and as such of the }
  1211. { temprefs coming after it) is set (necessary because the }
  1212. { temprefs will be part of the filepara, of which we need }
  1213. { the resultdef later on and temprefs can only be }
  1214. { typecheckpassed if the resultdef of the temp is known) }
  1215. typecheckpass(tnode(filetemp));
  1216. { assign the address of the file to the temp }
  1217. addstatement(newstatement,
  1218. cassignmentnode.create(ctemprefnode.create(filetemp),
  1219. caddrnode.create_internal(filepara.left)));
  1220. typecheckpass(newstatement.left);
  1221. { create a new fileparameter as follows: file_type(temp^) }
  1222. { (so that we pass the value and not the address of the temp }
  1223. { to the read/write routine) }
  1224. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  1225. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  1226. { replace the old file para with the new one }
  1227. filepara.left := nil;
  1228. filepara.free;
  1229. filepara := nextpara;
  1230. end;
  1231. end;
  1232. { the resultdef of the filepara must be set since it's }
  1233. { used below }
  1234. filepara.get_paratype;
  1235. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  1236. { if something goes wrong or at the end of the procedure }
  1237. { we're going to reuse the paranodes, so make sure they don't get freed }
  1238. { twice }
  1239. params:=Tcallparanode(left);
  1240. left := nil;
  1241. if is_typed then
  1242. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),tnode(newstatement))
  1243. else
  1244. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),tnode(newstatement));
  1245. { if we found an error, simply delete the generated blocknode }
  1246. if found_error then
  1247. newblock.free
  1248. else
  1249. begin
  1250. { deallocate the temp for the file para if we used one }
  1251. if assigned(filetemp) then
  1252. addstatement(newstatement,ctempdeletenode.create(filetemp));
  1253. { otherwise return the newly generated block of instructions, }
  1254. { but first free the errornode we generated at the beginning }
  1255. result.free;
  1256. result := newblock
  1257. end;
  1258. end;
  1259. function get_val_int_func(def: tdef): string;
  1260. var
  1261. ordtype: tordtype;
  1262. begin
  1263. ordtype := torddef(def).ordtype;
  1264. if not (ordtype in [s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  1265. internalerror(2013032603);
  1266. if is_oversizedint(def) then
  1267. begin
  1268. case ordtype of
  1269. s64bit: exit('int64');
  1270. u64bit: exit('qword');
  1271. s32bit: exit('longint');
  1272. u32bit: exit('longword');
  1273. s16bit: exit('smallint');
  1274. u16bit: exit('word');
  1275. else
  1276. internalerror(2013032604);
  1277. end;
  1278. end
  1279. else
  1280. begin
  1281. case ordtype of
  1282. s64bit,s32bit,s16bit,s8bit: exit('sint');
  1283. u64bit,u32bit,u16bit,u8bit: exit('uint');
  1284. else
  1285. internalerror(2013032604);
  1286. end;
  1287. end;
  1288. internalerror(2013032605);
  1289. end;
  1290. function tinlinenode.handle_val: tnode;
  1291. var
  1292. procname,
  1293. suffix : string[31];
  1294. sourcepara,
  1295. destpara,
  1296. codepara,
  1297. sizepara,
  1298. newparas : tcallparanode;
  1299. orgcode,tc : tnode;
  1300. newstatement : tstatementnode;
  1301. newblock : tblocknode;
  1302. tempcode : ttempcreatenode;
  1303. begin
  1304. { for easy exiting if something goes wrong }
  1305. result := cerrornode.create;
  1306. { check the amount of parameters }
  1307. if not(assigned(left)) or
  1308. not(assigned(tcallparanode(left).right)) then
  1309. begin
  1310. CGMessage1(parser_e_wrong_parameter_size,'Val');
  1311. exit;
  1312. end;
  1313. suffix:='';
  1314. { in case we are in a generic definition, we cannot
  1315. do all checks, the parameters might be type parameters }
  1316. if df_generic in current_procinfo.procdef.defoptions then
  1317. begin
  1318. result.Free;
  1319. result:=nil;
  1320. resultdef:=voidtype;
  1321. exit;
  1322. end;
  1323. { reverse parameters for easier processing }
  1324. left := reverseparameters(tcallparanode(left));
  1325. { get the parameters }
  1326. tempcode := nil;
  1327. orgcode := nil;
  1328. sizepara := nil;
  1329. sourcepara := tcallparanode(left);
  1330. destpara := tcallparanode(sourcepara.right);
  1331. codepara := tcallparanode(destpara.right);
  1332. { check if codepara is valid }
  1333. if assigned(codepara) and
  1334. (
  1335. not is_integer(codepara.resultdef)
  1336. {$ifndef cpu64bitaddr}
  1337. or is_64bitint(codepara.resultdef)
  1338. {$endif not cpu64bitaddr}
  1339. ) then
  1340. begin
  1341. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1342. exit;
  1343. end;
  1344. { check if dest para is valid }
  1345. if not is_integer(destpara.resultdef) and
  1346. not is_currency(destpara.resultdef) and
  1347. not(destpara.resultdef.typ in [floatdef,enumdef]) then
  1348. begin
  1349. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1350. exit;
  1351. end;
  1352. { we're going to reuse the exisiting para's, so make sure they }
  1353. { won't be disposed }
  1354. left := nil;
  1355. { create the blocknode which will hold the generated statements + }
  1356. { an initial dummy statement }
  1357. newblock:=internalstatements(newstatement);
  1358. { do we need a temp for code? Yes, if no code specified, or if }
  1359. { code is not a 32bit parameter (we already checked whether the }
  1360. { the code para, if specified, was an orddef) }
  1361. if not assigned(codepara) or
  1362. (codepara.resultdef.size<>ptrsinttype.size) then
  1363. begin
  1364. tempcode := ctempcreatenode.create(ptrsinttype,ptrsinttype.size,tt_persistent,false);
  1365. addstatement(newstatement,tempcode);
  1366. { set the resultdef of the temp (needed to be able to get }
  1367. { the resultdef of the tempref used in the new code para) }
  1368. typecheckpass(tnode(tempcode));
  1369. { create a temp codepara, but save the original code para to }
  1370. { assign the result to later on }
  1371. if assigned(codepara) then
  1372. begin
  1373. orgcode := codepara.left;
  1374. codepara.left := ctemprefnode.create(tempcode);
  1375. end
  1376. else
  1377. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1378. { we need its resultdef later on }
  1379. codepara.get_paratype;
  1380. end
  1381. else if (torddef(codepara.resultdef).ordtype <> torddef(ptrsinttype).ordtype) then
  1382. { because code is a var parameter, it must match types exactly }
  1383. { however, since it will return values >= 0, both signed and }
  1384. { and unsigned ints of the same size are fine. Since the formal }
  1385. { code para type is sinttype, insert a typecoversion to sint for }
  1386. { unsigned para's }
  1387. begin
  1388. codepara.left := ctypeconvnode.create_internal(codepara.left,ptrsinttype);
  1389. { make it explicit, oterwise you may get a nonsense range }
  1390. { check error if the cardinal already contained a value }
  1391. { > $7fffffff }
  1392. codepara.get_paratype;
  1393. end;
  1394. { create the procedure name }
  1395. procname := 'fpc_val_';
  1396. case destpara.resultdef.typ of
  1397. orddef:
  1398. begin
  1399. case torddef(destpara.resultdef).ordtype of
  1400. s8bit,s16bit,s32bit,s64bit,
  1401. u8bit,u16bit,u32bit,u64bit:
  1402. begin
  1403. suffix := get_val_int_func(destpara.resultdef) + '_';
  1404. { we also need a destsize para in the case of sint }
  1405. if suffix = 'sint_' then
  1406. sizepara := ccallparanode.create(cordconstnode.create
  1407. (destpara.resultdef.size,s32inttype,true),nil);
  1408. end;
  1409. scurrency: suffix := 'currency_';
  1410. else
  1411. internalerror(200304225);
  1412. end;
  1413. end;
  1414. floatdef:
  1415. suffix:='real_';
  1416. enumdef:
  1417. begin
  1418. suffix:='enum_';
  1419. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1420. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1421. ),nil);
  1422. end;
  1423. end;
  1424. procname := procname + suffix;
  1425. { play a trick to have tcallnode handle invalid source parameters: }
  1426. { the shortstring-longint val routine by default }
  1427. if (sourcepara.resultdef.typ = stringdef) then
  1428. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1429. { zero-based arrays (of char) can be implicitely converted to ansistring, but don't do
  1430. so if not needed because the array is too short }
  1431. else if is_zero_based_array(sourcepara.resultdef) and (sourcepara.resultdef.size>255) then
  1432. procname := procname + 'ansistr'
  1433. else
  1434. procname := procname + 'shortstr';
  1435. { set up the correct parameters for the call: the code para... }
  1436. newparas := codepara;
  1437. { and the source para }
  1438. codepara.right := sourcepara;
  1439. { sizepara either contains nil if none is needed (which is ok, since }
  1440. { then the next statement severes any possible links with other paras }
  1441. { that sourcepara may have) or it contains the necessary size para and }
  1442. { its right field is nil }
  1443. sourcepara.right := sizepara;
  1444. { create the call and assign the result to dest (val helpers are functions).
  1445. Use a trick to prevent a type size mismatch warning to be generated by the
  1446. assignment node. First convert implicitly to the resultdef. This will insert
  1447. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1448. for the assignment node and therefor preventing the warning (PFV)
  1449. The implicit conversion is avoided for enums because implicit conversion between
  1450. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1451. possible. (DM).
  1452. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1453. to prevent warning about automatic type conversion. }
  1454. if (destpara.resultdef.typ=enumdef) or
  1455. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1456. then
  1457. tc:=ccallnode.createintern(procname,newparas)
  1458. else
  1459. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1460. addstatement(newstatement,cassignmentnode.create(
  1461. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1462. { dispose of the enclosing paranode of the destination }
  1463. destpara.left := nil;
  1464. destpara.right := nil;
  1465. destpara.free;
  1466. { check if we used a temp for code and whether we have to store }
  1467. { it to the real code parameter }
  1468. if assigned(orgcode) then
  1469. addstatement(newstatement,cassignmentnode.create(
  1470. orgcode,
  1471. ctypeconvnode.create_internal(
  1472. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1473. { release the temp if we allocated one }
  1474. if assigned(tempcode) then
  1475. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1476. { free the errornode }
  1477. result.free;
  1478. { and return it }
  1479. result := newblock;
  1480. end;
  1481. function tinlinenode.handle_setlength: tnode;
  1482. var
  1483. def: tdef;
  1484. destppn,
  1485. paras: tnode;
  1486. newstatement: tstatementnode;
  1487. ppn: tcallparanode;
  1488. counter,
  1489. dims: longint;
  1490. isarray: boolean;
  1491. begin
  1492. { for easy exiting if something goes wrong }
  1493. result:=cerrornode.create;
  1494. resultdef:=voidtype;
  1495. paras:=left;
  1496. dims:=0;
  1497. if assigned(paras) then
  1498. begin
  1499. { check type of lengths }
  1500. ppn:=tcallparanode(paras);
  1501. while assigned(ppn.right) do
  1502. begin
  1503. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1504. inserttypeconv(ppn.left,sinttype);
  1505. inc(dims);
  1506. ppn:=tcallparanode(ppn.right);
  1507. end;
  1508. end
  1509. else
  1510. internalerror(2013112912);
  1511. if dims=0 then
  1512. begin
  1513. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1514. exit;
  1515. end;
  1516. { last param must be var }
  1517. destppn:=ppn.left;
  1518. valid_for_var(destppn,true);
  1519. set_varstate(destppn,vs_written,[]);
  1520. { first param must be a string or dynamic array ...}
  1521. isarray:=is_dynamic_array(destppn.resultdef);
  1522. if not((destppn.resultdef.typ=stringdef) or
  1523. isarray) then
  1524. begin
  1525. { possibly generic involved? }
  1526. if df_generic in current_procinfo.procdef.defoptions then
  1527. result:=internalstatements(newstatement)
  1528. else
  1529. CGMessage(type_e_mismatch);
  1530. exit;
  1531. end;
  1532. { only dynamic arrays accept more dimensions }
  1533. if (dims>1) then
  1534. begin
  1535. if (not isarray) then
  1536. CGMessage(type_e_mismatch)
  1537. else
  1538. begin
  1539. { check if the amount of dimensions is valid }
  1540. def:=tarraydef(destppn.resultdef).elementdef;
  1541. counter:=dims;
  1542. while counter > 1 do
  1543. begin
  1544. if not(is_dynamic_array(def)) then
  1545. begin
  1546. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1547. break;
  1548. end;
  1549. dec(counter);
  1550. def:=tarraydef(def).elementdef;
  1551. end;
  1552. end;
  1553. end;
  1554. result.free;
  1555. result:=nil;
  1556. end;
  1557. function tinlinenode.handle_copy: tnode;
  1558. var
  1559. paras : tnode;
  1560. ppn : tcallparanode;
  1561. paradef : tdef;
  1562. counter : integer;
  1563. begin
  1564. result:=nil;
  1565. { determine copy function to use based on the first argument,
  1566. also count the number of arguments in this loop }
  1567. counter:=1;
  1568. paras:=left;
  1569. ppn:=tcallparanode(paras);
  1570. while assigned(ppn.right) do
  1571. begin
  1572. inc(counter);
  1573. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1574. ppn:=tcallparanode(ppn.right);
  1575. end;
  1576. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1577. paradef:=ppn.left.resultdef;
  1578. if is_ansistring(paradef) then
  1579. // set resultdef to argument def
  1580. resultdef:=paradef
  1581. else if (is_chararray(paradef) and (paradef.size>255)) or
  1582. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1583. // set resultdef to ansistring type since result will be in ansistring codepage
  1584. resultdef:=getansistringdef
  1585. else
  1586. if is_widestring(paradef) then
  1587. resultdef:=cwidestringtype
  1588. else
  1589. if is_unicodestring(paradef) or
  1590. is_widechararray(paradef) or
  1591. is_pwidechar(paradef) then
  1592. resultdef:=cunicodestringtype
  1593. else
  1594. if is_char(paradef) then
  1595. resultdef:=cshortstringtype
  1596. else
  1597. if is_dynamic_array(paradef) then
  1598. begin
  1599. { Only allow 1 or 3 arguments }
  1600. if not(counter in [1..3]) then
  1601. begin
  1602. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  1603. exit;
  1604. end;
  1605. resultdef:=paradef;
  1606. end
  1607. else
  1608. begin
  1609. { generic fallback that will give an error if a wrong
  1610. type is passed }
  1611. if (counter=3) then
  1612. resultdef:=cshortstringtype
  1613. else
  1614. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  1615. end;
  1616. end;
  1617. {$maxfpuregisters 0}
  1618. function getpi : bestreal;
  1619. begin
  1620. {$ifdef x86}
  1621. { x86 has pi in hardware }
  1622. result:=pi;
  1623. {$else x86}
  1624. {$ifdef cpuextended}
  1625. result:=MathPiExtended.Value;
  1626. {$else cpuextended}
  1627. result:=MathPi.Value;
  1628. {$endif cpuextended}
  1629. {$endif x86}
  1630. end;
  1631. function tinlinenode.simplify(forinline : boolean): tnode;
  1632. function do_lowhigh(def:tdef) : tnode;
  1633. var
  1634. v : tconstexprint;
  1635. enum : tenumsym;
  1636. hp : tnode;
  1637. i : integer;
  1638. begin
  1639. case def.typ of
  1640. orddef:
  1641. begin
  1642. set_varstate(left,vs_read,[]);
  1643. if inlinenumber=in_low_x then
  1644. v:=torddef(def).low
  1645. else
  1646. v:=torddef(def).high;
  1647. hp:=cordconstnode.create(v,def,true);
  1648. typecheckpass(hp);
  1649. do_lowhigh:=hp;
  1650. end;
  1651. enumdef:
  1652. begin
  1653. set_varstate(left,vs_read,[]);
  1654. if inlinenumber=in_high_x then
  1655. v:=tenumdef(def).maxval
  1656. else
  1657. v:=tenumdef(def).minval;
  1658. enum:=nil;
  1659. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1660. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1661. begin
  1662. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1663. break;
  1664. end;
  1665. if not assigned(enum) then
  1666. internalerror(309993)
  1667. else
  1668. hp:=genenumnode(enum);
  1669. do_lowhigh:=hp;
  1670. end;
  1671. else
  1672. internalerror(87);
  1673. end;
  1674. end;
  1675. function getconstrealvalue : bestreal;
  1676. begin
  1677. case left.nodetype of
  1678. ordconstn:
  1679. getconstrealvalue:=tordconstnode(left).value;
  1680. realconstn:
  1681. getconstrealvalue:=trealconstnode(left).value_real;
  1682. else
  1683. internalerror(309992);
  1684. end;
  1685. end;
  1686. procedure setconstrealvalue(r : bestreal);
  1687. begin
  1688. result:=crealconstnode.create(r,pbestrealtype^);
  1689. end;
  1690. function handle_ln_const(r : bestreal) : tnode;
  1691. begin
  1692. if r<=0.0 then
  1693. if floating_point_range_check_error then
  1694. begin
  1695. result:=crealconstnode.create(0,pbestrealtype^);
  1696. CGMessage(type_e_wrong_math_argument)
  1697. end
  1698. else
  1699. begin
  1700. if r=0.0 then
  1701. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1702. else
  1703. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1704. end
  1705. else
  1706. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1707. end;
  1708. function handle_sqrt_const(r : bestreal) : tnode;
  1709. begin
  1710. if r<0.0 then
  1711. if floating_point_range_check_error then
  1712. begin
  1713. result:=crealconstnode.create(0,pbestrealtype^);
  1714. CGMessage(type_e_wrong_math_argument)
  1715. end
  1716. else
  1717. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1718. else
  1719. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1720. end;
  1721. function handle_const_sar : tnode;
  1722. var
  1723. vl,vl2 : TConstExprInt;
  1724. bits,shift: integer;
  1725. mask : qword;
  1726. def : tdef;
  1727. begin
  1728. result:=nil;
  1729. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1730. begin
  1731. if (left.nodetype=callparan) and
  1732. assigned(tcallparanode(left).right) then
  1733. begin
  1734. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1735. begin
  1736. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1737. vl:=tordconstnode(tcallparanode(left).left).value;
  1738. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1739. end
  1740. else
  1741. exit;
  1742. end
  1743. else
  1744. begin
  1745. def:=left.resultdef;
  1746. vl:=1;
  1747. vl2:=tordconstnode(left).value;
  1748. end;
  1749. bits:=def.size*8;
  1750. shift:=vl.svalue and (bits-1);
  1751. case bits of
  1752. 8:
  1753. mask:=$ff;
  1754. 16:
  1755. mask:=$ffff;
  1756. 32:
  1757. mask:=$ffffffff;
  1758. 64:
  1759. mask:=qword($ffffffffffffffff);
  1760. else
  1761. mask:=qword(1 shl bits)-1;
  1762. end;
  1763. {$push}
  1764. {$r-,q-}
  1765. if shift=0 then
  1766. result:=cordconstnode.create(vl2.svalue,def,false)
  1767. else if vl2.svalue<0 then
  1768. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1769. else
  1770. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1771. {$pop}
  1772. end
  1773. else
  1774. end;
  1775. function handle_const_rox : tnode;
  1776. var
  1777. vl,vl2 : TConstExprInt;
  1778. bits,shift: integer;
  1779. def : tdef;
  1780. begin
  1781. result:=nil;
  1782. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1783. begin
  1784. if (left.nodetype=callparan) and
  1785. assigned(tcallparanode(left).right) then
  1786. begin
  1787. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1788. begin
  1789. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1790. vl:=tordconstnode(tcallparanode(left).left).value;
  1791. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1792. end
  1793. else
  1794. exit;
  1795. end
  1796. else
  1797. begin
  1798. def:=left.resultdef;
  1799. vl:=1;
  1800. vl2:=tordconstnode(left).value;
  1801. end;
  1802. bits:=def.size*8;
  1803. shift:=vl.svalue and (bits-1);
  1804. {$push}
  1805. {$r-,q-}
  1806. if shift=0 then
  1807. result:=cordconstnode.create(vl2.svalue,def,false)
  1808. else
  1809. case inlinenumber of
  1810. in_ror_x,in_ror_x_y:
  1811. case def.size of
  1812. 1:
  1813. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1814. 2:
  1815. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1816. 4:
  1817. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1818. 8:
  1819. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1820. else
  1821. internalerror(2011061903);
  1822. end;
  1823. in_rol_x,in_rol_x_y:
  1824. case def.size of
  1825. 1:
  1826. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1827. 2:
  1828. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1829. 4:
  1830. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1831. 8:
  1832. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1833. else
  1834. internalerror(2011061902);
  1835. end;
  1836. else
  1837. internalerror(2011061901);
  1838. end;
  1839. end;
  1840. end;
  1841. var
  1842. hp : tnode;
  1843. vl,vl2 : TConstExprInt;
  1844. vr : bestreal;
  1845. begin { simplify }
  1846. result:=nil;
  1847. { handle intern constant functions in separate case }
  1848. if nf_inlineconst in flags then
  1849. begin
  1850. { no parameters? }
  1851. if not assigned(left) then
  1852. internalerror(200501231)
  1853. else
  1854. begin
  1855. vl:=0;
  1856. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1857. case left.nodetype of
  1858. realconstn :
  1859. begin
  1860. { Real functions are all handled with internproc below }
  1861. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1862. end;
  1863. ordconstn :
  1864. vl:=tordconstnode(left).value;
  1865. callparan :
  1866. begin
  1867. { both exists, else it was not generated }
  1868. vl:=tordconstnode(tcallparanode(left).left).value;
  1869. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1870. end;
  1871. else
  1872. CGMessage(parser_e_illegal_expression);
  1873. end;
  1874. case inlinenumber of
  1875. in_const_abs :
  1876. if vl.signed then
  1877. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1878. else
  1879. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1880. in_const_sqr:
  1881. if vl.signed then
  1882. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1883. else
  1884. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1885. in_const_odd :
  1886. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1887. in_const_swap_word :
  1888. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1889. in_const_swap_long :
  1890. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1891. in_const_swap_qword :
  1892. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1893. in_const_ptr:
  1894. begin
  1895. {Don't construct pointers from negative values.}
  1896. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1897. cgmessage(parser_e_range_check_error);
  1898. {$if defined(i8086)}
  1899. hp:=cpointerconstnode.create((vl2.uvalue shl 16)+vl.uvalue,voidfarpointertype);
  1900. {$elseif defined(i386)}
  1901. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidnearfspointertype);
  1902. {$else}
  1903. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidpointertype);
  1904. {$endif}
  1905. end
  1906. else
  1907. internalerror(88);
  1908. end;
  1909. end;
  1910. if hp=nil then
  1911. hp:=cerrornode.create;
  1912. result:=hp;
  1913. end
  1914. else
  1915. begin
  1916. case inlinenumber of
  1917. in_lo_long,
  1918. in_hi_long,
  1919. in_lo_qword,
  1920. in_hi_qword,
  1921. in_lo_word,
  1922. in_hi_word :
  1923. begin
  1924. if left.nodetype=ordconstn then
  1925. begin
  1926. case inlinenumber of
  1927. in_lo_word :
  1928. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1929. in_hi_word :
  1930. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1931. in_lo_long :
  1932. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1933. in_hi_long :
  1934. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1935. in_lo_qword :
  1936. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1937. in_hi_qword :
  1938. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1939. end;
  1940. end;
  1941. end;
  1942. in_ord_x:
  1943. begin
  1944. case left.resultdef.typ of
  1945. orddef :
  1946. begin
  1947. case torddef(left.resultdef).ordtype of
  1948. pasbool8,
  1949. uchar:
  1950. begin
  1951. { change to byte() }
  1952. result:=ctypeconvnode.create_internal(left,u8inttype);
  1953. left:=nil;
  1954. end;
  1955. pasbool16,
  1956. uwidechar :
  1957. begin
  1958. { change to word() }
  1959. result:=ctypeconvnode.create_internal(left,u16inttype);
  1960. left:=nil;
  1961. end;
  1962. pasbool32 :
  1963. begin
  1964. { change to dword() }
  1965. result:=ctypeconvnode.create_internal(left,u32inttype);
  1966. left:=nil;
  1967. end;
  1968. pasbool64 :
  1969. begin
  1970. { change to qword() }
  1971. result:=ctypeconvnode.create_internal(left,u64inttype);
  1972. left:=nil;
  1973. end;
  1974. bool8bit:
  1975. begin
  1976. { change to shortint() }
  1977. result:=ctypeconvnode.create_internal(left,s8inttype);
  1978. left:=nil;
  1979. end;
  1980. bool16bit :
  1981. begin
  1982. { change to smallint() }
  1983. result:=ctypeconvnode.create_internal(left,s16inttype);
  1984. left:=nil;
  1985. end;
  1986. bool32bit :
  1987. begin
  1988. { change to longint() }
  1989. result:=ctypeconvnode.create_internal(left,s32inttype);
  1990. left:=nil;
  1991. end;
  1992. bool64bit :
  1993. begin
  1994. { change to int64() }
  1995. result:=ctypeconvnode.create_internal(left,s64inttype);
  1996. left:=nil;
  1997. end;
  1998. uvoid :
  1999. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2000. else
  2001. begin
  2002. { all other orddef need no transformation }
  2003. result:=left;
  2004. left:=nil;
  2005. end;
  2006. end;
  2007. end;
  2008. enumdef :
  2009. begin
  2010. result:=ctypeconvnode.create_internal(left,s32inttype);
  2011. left:=nil;
  2012. end;
  2013. pointerdef :
  2014. begin
  2015. if m_mac in current_settings.modeswitches then
  2016. begin
  2017. result:=ctypeconvnode.create_internal(left,ptruinttype);
  2018. left:=nil;
  2019. end
  2020. end;
  2021. end;
  2022. (*
  2023. if (left.nodetype=ordconstn) then
  2024. begin
  2025. result:=cordconstnode.create(
  2026. tordconstnode(left).value,sinttype,true);
  2027. end
  2028. else if (m_mac in current_settings.modeswitches) and
  2029. (left.ndoetype=pointerconstn) then
  2030. result:=cordconstnode.create(
  2031. tpointerconstnode(left).value,ptruinttype,true);
  2032. *)
  2033. end;
  2034. in_chr_byte:
  2035. begin
  2036. { convert to explicit char() }
  2037. result:=ctypeconvnode.create_internal(left,cansichartype);
  2038. left:=nil;
  2039. end;
  2040. in_length_x:
  2041. begin
  2042. case left.resultdef.typ of
  2043. stringdef :
  2044. begin
  2045. if (left.nodetype=stringconstn) then
  2046. begin
  2047. result:=cordconstnode.create(
  2048. tstringconstnode(left).len,sinttype,true);
  2049. end;
  2050. end;
  2051. orddef :
  2052. begin
  2053. { length of char is always one }
  2054. if is_char(left.resultdef) or
  2055. is_widechar(left.resultdef) then
  2056. begin
  2057. result:=cordconstnode.create(1,sinttype,false);
  2058. end
  2059. end;
  2060. arraydef :
  2061. begin
  2062. if (left.nodetype=stringconstn) then
  2063. begin
  2064. result:=cordconstnode.create(
  2065. tstringconstnode(left).len,sinttype,true);
  2066. end
  2067. else if not is_open_array(left.resultdef) and
  2068. not is_array_of_const(left.resultdef) and
  2069. not is_dynamic_array(left.resultdef) then
  2070. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  2071. tarraydef(left.resultdef).lowrange+1,
  2072. sinttype,true);
  2073. end;
  2074. end;
  2075. end;
  2076. in_assigned_x:
  2077. begin
  2078. if is_constnode(tcallparanode(left).left) or
  2079. (tcallparanode(left).left.nodetype = pointerconstn) then
  2080. begin
  2081. { let an add node figure it out }
  2082. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  2083. tcallparanode(left).left := nil;
  2084. end;
  2085. end;
  2086. in_pred_x,
  2087. in_succ_x:
  2088. begin
  2089. if (left.nodetype=ordconstn) then
  2090. begin
  2091. if (inlinenumber=in_succ_x) then
  2092. vl:=tordconstnode(left).value+1
  2093. else
  2094. vl:=tordconstnode(left).value-1;
  2095. if is_integer(left.resultdef) then
  2096. { the type of the original integer constant is irrelevant,
  2097. it should be automatically adapted to the new value
  2098. (except when inlining) }
  2099. result:=create_simplified_ord_const(vl,resultdef,forinline)
  2100. else
  2101. { check the range for enums, chars, booleans }
  2102. result:=cordconstnode.create(vl,left.resultdef,true)
  2103. end
  2104. end;
  2105. in_low_x,
  2106. in_high_x:
  2107. begin
  2108. case left.resultdef.typ of
  2109. orddef,
  2110. enumdef:
  2111. begin
  2112. result:=do_lowhigh(left.resultdef);
  2113. end;
  2114. setdef:
  2115. begin
  2116. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  2117. end;
  2118. arraydef:
  2119. begin
  2120. if (inlinenumber=in_low_x) then
  2121. begin
  2122. result:=cordconstnode.create(int64(tarraydef(
  2123. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2124. end
  2125. else if not is_open_array(left.resultdef) and
  2126. not is_array_of_const(left.resultdef) and
  2127. not is_dynamic_array(left.resultdef) then
  2128. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2129. tarraydef(left.resultdef).rangedef,true);
  2130. end;
  2131. stringdef:
  2132. begin
  2133. if inlinenumber=in_low_x then
  2134. begin
  2135. if is_dynamicstring(left.resultdef) and
  2136. not(cs_zerobasedstrings in current_settings.localswitches) then
  2137. result:=cordconstnode.create(1,u8inttype,false)
  2138. else
  2139. result:=cordconstnode.create(0,u8inttype,false);
  2140. end
  2141. else if not is_dynamicstring(left.resultdef) then
  2142. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2143. end;
  2144. end;
  2145. end;
  2146. in_exp_real :
  2147. begin
  2148. if left.nodetype in [ordconstn,realconstn] then
  2149. begin
  2150. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2151. if (trealconstnode(result).value_real=MathInf.Value) and
  2152. floating_point_range_check_error then
  2153. begin
  2154. result:=crealconstnode.create(0,pbestrealtype^);
  2155. CGMessage(parser_e_range_check_error);
  2156. end;
  2157. end
  2158. end;
  2159. in_trunc_real :
  2160. begin
  2161. if left.nodetype in [ordconstn,realconstn] then
  2162. begin
  2163. vr:=getconstrealvalue;
  2164. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2165. begin
  2166. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2167. result:=cordconstnode.create(1,s64inttype,false)
  2168. end
  2169. else
  2170. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2171. end
  2172. end;
  2173. in_round_real :
  2174. begin
  2175. { can't evaluate while inlining, may depend on fpu setting }
  2176. if (not forinline) and
  2177. (left.nodetype in [ordconstn,realconstn]) then
  2178. begin
  2179. vr:=getconstrealvalue;
  2180. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2181. begin
  2182. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2183. result:=cordconstnode.create(1,s64inttype,false)
  2184. end
  2185. else
  2186. result:=cordconstnode.create(round(vr),s64inttype,true)
  2187. end
  2188. end;
  2189. in_frac_real :
  2190. begin
  2191. if left.nodetype in [ordconstn,realconstn] then
  2192. setconstrealvalue(frac(getconstrealvalue))
  2193. end;
  2194. in_int_real :
  2195. begin
  2196. if left.nodetype in [ordconstn,realconstn] then
  2197. setconstrealvalue(int(getconstrealvalue));
  2198. end;
  2199. in_pi_real :
  2200. begin
  2201. if block_type=bt_const then
  2202. setconstrealvalue(getpi)
  2203. end;
  2204. in_cos_real :
  2205. begin
  2206. if left.nodetype in [ordconstn,realconstn] then
  2207. setconstrealvalue(cos(getconstrealvalue))
  2208. end;
  2209. in_sin_real :
  2210. begin
  2211. if left.nodetype in [ordconstn,realconstn] then
  2212. setconstrealvalue(sin(getconstrealvalue))
  2213. end;
  2214. in_arctan_real :
  2215. begin
  2216. if left.nodetype in [ordconstn,realconstn] then
  2217. setconstrealvalue(arctan(getconstrealvalue))
  2218. end;
  2219. in_abs_real :
  2220. begin
  2221. if left.nodetype in [ordconstn,realconstn] then
  2222. setconstrealvalue(abs(getconstrealvalue))
  2223. end;
  2224. in_abs_long:
  2225. begin
  2226. if left.nodetype=ordconstn then
  2227. begin
  2228. if tordconstnode(left).value<0 then
  2229. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  2230. else
  2231. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  2232. end
  2233. end;
  2234. in_sqr_real :
  2235. begin
  2236. if left.nodetype in [ordconstn,realconstn] then
  2237. setconstrealvalue(sqr(getconstrealvalue))
  2238. end;
  2239. in_sqrt_real :
  2240. begin
  2241. if left.nodetype in [ordconstn,realconstn] then
  2242. begin
  2243. vr:=getconstrealvalue;
  2244. if vr<0.0 then
  2245. result:=handle_sqrt_const(vr)
  2246. else
  2247. setconstrealvalue(sqrt(vr));
  2248. end
  2249. end;
  2250. in_ln_real :
  2251. begin
  2252. if left.nodetype in [ordconstn,realconstn] then
  2253. begin
  2254. vr:=getconstrealvalue;
  2255. if vr<=0.0 then
  2256. result:=handle_ln_const(vr)
  2257. else
  2258. setconstrealvalue(ln(vr));
  2259. end
  2260. end;
  2261. in_assert_x_y :
  2262. begin
  2263. if not(cs_do_assertion in current_settings.localswitches) then
  2264. { we need a valid node, so insert a nothingn }
  2265. result:=cnothingnode.create;
  2266. end;
  2267. in_sar_x,
  2268. in_sar_x_y :
  2269. begin
  2270. result:=handle_const_sar;
  2271. end;
  2272. in_rol_x,
  2273. in_rol_x_y,
  2274. in_ror_x,
  2275. in_ror_x_y :
  2276. result:=handle_const_rox;
  2277. end;
  2278. end;
  2279. end;
  2280. function tinlinenode.pass_typecheck:tnode;
  2281. procedure setfloatresultdef;
  2282. begin
  2283. if (left.resultdef.typ=floatdef) and
  2284. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2285. resultdef:=left.resultdef
  2286. else
  2287. begin
  2288. if (left.nodetype <> ordconstn) then
  2289. inserttypeconv(left,pbestrealtype^);
  2290. resultdef:=pbestrealtype^;
  2291. end;
  2292. end;
  2293. procedure handle_pack_unpack;
  2294. var
  2295. source, target, index: tcallparanode;
  2296. unpackedarraydef, packedarraydef: tarraydef;
  2297. tempindex: TConstExprInt;
  2298. begin
  2299. resultdef:=voidtype;
  2300. unpackedarraydef := nil;
  2301. packedarraydef := nil;
  2302. source := tcallparanode(left);
  2303. if (inlinenumber = in_unpack_x_y_z) then
  2304. begin
  2305. target := tcallparanode(source.right);
  2306. index := tcallparanode(target.right);
  2307. { source must be a packed array }
  2308. if not is_packed_array(source.left.resultdef) then
  2309. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2310. else
  2311. packedarraydef := tarraydef(source.left.resultdef);
  2312. { target can be any kind of array, as long as it's not packed }
  2313. if (target.left.resultdef.typ <> arraydef) or
  2314. is_packed_array(target.left.resultdef) then
  2315. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2316. else
  2317. unpackedarraydef := tarraydef(target.left.resultdef);
  2318. end
  2319. else
  2320. begin
  2321. index := tcallparanode(source.right);
  2322. target := tcallparanode(index.right);
  2323. { source can be any kind of array, as long as it's not packed }
  2324. if (source.left.resultdef.typ <> arraydef) or
  2325. is_packed_array(source.left.resultdef) then
  2326. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2327. else
  2328. unpackedarraydef := tarraydef(source.left.resultdef);
  2329. { target must be a packed array }
  2330. if not is_packed_array(target.left.resultdef) then
  2331. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2332. else
  2333. packedarraydef := tarraydef(target.left.resultdef);
  2334. end;
  2335. if assigned(unpackedarraydef) then
  2336. begin
  2337. { index must be compatible with the unpacked array's indextype }
  2338. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2339. { range check at compile time if possible }
  2340. if assigned(packedarraydef) and
  2341. (index.left.nodetype = ordconstn) and
  2342. not is_special_array(unpackedarraydef) then
  2343. begin
  2344. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2345. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2346. testrange(unpackedarraydef,tempindex,false,false);
  2347. end;
  2348. end;
  2349. { source array is read and must be valid }
  2350. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2351. { target array is written }
  2352. valid_for_assignment(target.left,true);
  2353. set_varstate(target.left,vs_written,[]);
  2354. { index in the unpacked array is read and must be valid }
  2355. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2356. { if the size of the arrays is 0 (array of empty records), }
  2357. { do nothing }
  2358. if (source.resultdef.size = 0) then
  2359. result:=cnothingnode.create;
  2360. end;
  2361. function handle_objc_encode: tnode;
  2362. var
  2363. encodedtype: ansistring;
  2364. errordef: tdef;
  2365. begin
  2366. encodedtype:='';
  2367. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2368. Message1(type_e_objc_type_unsupported,errordef.typename);
  2369. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype),nil);
  2370. end;
  2371. var
  2372. hightree,
  2373. hp : tnode;
  2374. temp_pnode: pnode;
  2375. begin
  2376. result:=nil;
  2377. { when handling writeln "left" contains no valid address }
  2378. if assigned(left) then
  2379. begin
  2380. if left.nodetype=callparan then
  2381. tcallparanode(left).get_paratype
  2382. else
  2383. typecheckpass(left);
  2384. end;
  2385. if not(nf_inlineconst in flags) then
  2386. begin
  2387. case inlinenumber of
  2388. in_lo_long,
  2389. in_hi_long,
  2390. in_lo_qword,
  2391. in_hi_qword,
  2392. in_lo_word,
  2393. in_hi_word :
  2394. begin
  2395. { give warning for incompatibility with tp and delphi }
  2396. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2397. ((m_tp7 in current_settings.modeswitches) or
  2398. (m_delphi in current_settings.modeswitches)) then
  2399. CGMessage(type_w_maybe_wrong_hi_lo);
  2400. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2401. if not is_integer(left.resultdef) then
  2402. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2403. case inlinenumber of
  2404. in_lo_word,
  2405. in_hi_word :
  2406. resultdef:=u8inttype;
  2407. in_lo_long,
  2408. in_hi_long :
  2409. resultdef:=u16inttype;
  2410. in_lo_qword,
  2411. in_hi_qword :
  2412. resultdef:=u32inttype;
  2413. end;
  2414. end;
  2415. in_sizeof_x:
  2416. begin
  2417. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2418. set_varstate(left,vs_read,[]);
  2419. if (left.resultdef.typ<>undefineddef) and
  2420. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2421. begin
  2422. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2423. if assigned(hightree) then
  2424. begin
  2425. hp:=caddnode.create(addn,hightree,
  2426. cordconstnode.create(1,sinttype,false));
  2427. if (left.resultdef.typ=arraydef) then
  2428. if not is_packed_array(tarraydef(left.resultdef)) then
  2429. begin
  2430. if (tarraydef(left.resultdef).elesize<>1) then
  2431. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2432. left.resultdef).elesize,sinttype,true));
  2433. end
  2434. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2435. begin
  2436. { no packed open array support yet }
  2437. if (hp.nodetype <> ordconstn) then
  2438. internalerror(2006081511);
  2439. hp.free;
  2440. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2441. {
  2442. hp:=
  2443. ctypeconvnode.create_explicit(sinttype,
  2444. cmoddivnode.create(divn,
  2445. caddnode.create(addn,
  2446. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2447. left.resultdef).elepackedbitsize,s64inttype,true)),
  2448. cordconstnode.create(a,s64inttype,true)),
  2449. cordconstnode.create(8,s64inttype,true)),
  2450. sinttype);
  2451. }
  2452. end;
  2453. result:=hp;
  2454. end;
  2455. end
  2456. else
  2457. resultdef:=sinttype;
  2458. end;
  2459. in_typeof_x:
  2460. begin
  2461. if target_info.system in systems_managed_vm then
  2462. message(parser_e_feature_unsupported_for_vm);
  2463. typecheckpass(left);
  2464. set_varstate(left,vs_read,[]);
  2465. if (left.resultdef.typ=objectdef) and
  2466. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2467. message(type_e_typeof_requires_vmt);
  2468. resultdef:=voidpointertype;
  2469. end;
  2470. in_ord_x:
  2471. begin
  2472. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2473. case left.resultdef.typ of
  2474. orddef,
  2475. enumdef :
  2476. ;
  2477. pointerdef :
  2478. begin
  2479. if not(m_mac in current_settings.modeswitches) then
  2480. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2481. end
  2482. else
  2483. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2484. end;
  2485. end;
  2486. in_chr_byte:
  2487. begin
  2488. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2489. end;
  2490. in_length_x:
  2491. begin
  2492. if ((left.resultdef.typ=arraydef) and
  2493. (not is_special_array(left.resultdef) or
  2494. is_open_array(left.resultdef))) or
  2495. (left.resultdef.typ=orddef) then
  2496. set_varstate(left,vs_read,[])
  2497. else
  2498. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2499. case left.resultdef.typ of
  2500. variantdef:
  2501. begin
  2502. inserttypeconv(left,getansistringdef);
  2503. end;
  2504. stringdef :
  2505. begin
  2506. { we don't need string convertions here, }
  2507. { except if from widestring to ansistring }
  2508. { and vice versa (that can change the }
  2509. { length) }
  2510. if (left.nodetype=typeconvn) and
  2511. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2512. not(is_wide_or_unicode_string(left.resultdef) xor
  2513. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2514. begin
  2515. hp:=ttypeconvnode(left).left;
  2516. ttypeconvnode(left).left:=nil;
  2517. left.free;
  2518. left:=hp;
  2519. end;
  2520. end;
  2521. orddef :
  2522. begin
  2523. { will be handled in simplify }
  2524. if not is_char(left.resultdef) and
  2525. not is_widechar(left.resultdef) then
  2526. CGMessage(type_e_mismatch);
  2527. end;
  2528. pointerdef :
  2529. begin
  2530. if is_pchar(left.resultdef) then
  2531. begin
  2532. hp := ccallparanode.create(left,nil);
  2533. result := ccallnode.createintern('fpc_pchar_length',hp);
  2534. { make sure the left node doesn't get disposed, since it's }
  2535. { reused in the new node (JM) }
  2536. left:=nil;
  2537. exit;
  2538. end
  2539. else if is_pwidechar(left.resultdef) then
  2540. begin
  2541. hp := ccallparanode.create(left,nil);
  2542. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2543. { make sure the left node doesn't get disposed, since it's }
  2544. { reused in the new node (JM) }
  2545. left:=nil;
  2546. exit;
  2547. end
  2548. else
  2549. CGMessage(type_e_mismatch);
  2550. end;
  2551. arraydef :
  2552. begin
  2553. if is_open_array(left.resultdef) or
  2554. is_array_of_const(left.resultdef) then
  2555. begin
  2556. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2557. if assigned(hightree) then
  2558. result:=caddnode.create(addn,hightree,
  2559. cordconstnode.create(1,sinttype,false));
  2560. exit;
  2561. end
  2562. { Length() for dynamic arrays is inlined }
  2563. else
  2564. begin
  2565. { will be handled in simplify }
  2566. end;
  2567. end
  2568. else
  2569. CGMessage(type_e_mismatch);
  2570. end;
  2571. { shortstring return an 8 bit value as the length
  2572. is the first byte of the string }
  2573. if is_shortstring(left.resultdef) then
  2574. resultdef:=u8inttype
  2575. else
  2576. resultdef:=sinttype;
  2577. end;
  2578. in_typeinfo_x:
  2579. begin
  2580. if target_info.system in systems_managed_vm then
  2581. message(parser_e_feature_unsupported_for_vm);
  2582. if (left.resultdef.typ=enumdef) and
  2583. (tenumdef(left.resultdef).has_jumps) then
  2584. CGMessage(type_e_no_type_info);
  2585. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2586. resultdef:=voidpointertype;
  2587. end;
  2588. in_assigned_x:
  2589. begin
  2590. { the parser has already made sure the expression is valid }
  2591. { in case of a complex procvar, only check the "code" pointer }
  2592. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2593. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2594. begin
  2595. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2596. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2597. tcallparanode(left).get_paratype;
  2598. end;
  2599. { Postpone conversion into addnode until firstpass, so targets
  2600. may override first_assigned and insert specific code. }
  2601. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2602. resultdef:=pasbool8type;
  2603. end;
  2604. in_ofs_x :
  2605. internalerror(2000101001);
  2606. in_seg_x :
  2607. begin
  2608. result := typecheck_seg;
  2609. end;
  2610. in_pred_x,
  2611. in_succ_x:
  2612. begin
  2613. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2614. resultdef:=left.resultdef;
  2615. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  2616. begin
  2617. if (resultdef.typ=enumdef) and
  2618. (tenumdef(resultdef).has_jumps) and
  2619. not(m_delphi in current_settings.modeswitches) then
  2620. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2621. end
  2622. else
  2623. CGMessage(type_e_ordinal_expr_expected)
  2624. end;
  2625. in_copy_x:
  2626. result:=handle_copy;
  2627. in_initialize_x,
  2628. in_finalize_x:
  2629. begin
  2630. { inlined from pinline }
  2631. internalerror(200204231);
  2632. end;
  2633. in_setlength_x:
  2634. begin
  2635. result:=handle_setlength;
  2636. end;
  2637. in_inc_x,
  2638. in_dec_x:
  2639. begin
  2640. resultdef:=voidtype;
  2641. if not(df_generic in current_procinfo.procdef.defoptions) then
  2642. begin
  2643. if assigned(left) then
  2644. begin
  2645. { first param must be var }
  2646. valid_for_var(tcallparanode(left).left,true);
  2647. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2648. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2649. is_ordinal(left.resultdef) or
  2650. is_currency(left.resultdef) then
  2651. begin
  2652. { value of left gets changed -> must be unique }
  2653. set_unique(tcallparanode(left).left);
  2654. { two paras ? }
  2655. if assigned(tcallparanode(left).right) then
  2656. begin
  2657. if is_integer(tcallparanode(left).right.resultdef) then
  2658. begin
  2659. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2660. { when range/overflow checking is on, we
  2661. convert this to a regular add, and for proper
  2662. checking we need the original type }
  2663. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2664. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2665. begin
  2666. { don't convert values added to pointers into the pointer types themselves,
  2667. because that will turn signed values into unsigned ones, which then
  2668. goes wrong when they have to be multiplied with the size of the elements
  2669. to which the pointer points in ncginl (mantis #17342) }
  2670. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2671. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2672. else
  2673. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2674. end
  2675. else if is_integer(tcallparanode(left).left.resultdef) then
  2676. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2677. else
  2678. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2679. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2680. { should be handled in the parser (JM) }
  2681. internalerror(2006020901);
  2682. end
  2683. else
  2684. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2685. end;
  2686. end
  2687. { generic type parameter? }
  2688. else if is_typeparam(left.resultdef) then
  2689. begin
  2690. result:=cnothingnode.create;
  2691. exit;
  2692. end
  2693. else
  2694. begin
  2695. hp:=self;
  2696. if isunaryoverloaded(hp) then
  2697. begin
  2698. { inc(rec) and dec(rec) assigns result value to argument }
  2699. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2700. exit;
  2701. end
  2702. else
  2703. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2704. end;
  2705. end
  2706. else
  2707. CGMessagePos(fileinfo,type_e_mismatch);
  2708. end;
  2709. end;
  2710. in_read_x,
  2711. in_readln_x,
  2712. in_readstr_x,
  2713. in_write_x,
  2714. in_writeln_x,
  2715. in_writestr_x :
  2716. begin
  2717. result := handle_read_write;
  2718. end;
  2719. in_settextbuf_file_x :
  2720. begin
  2721. if target_info.system in systems_managed_vm then
  2722. message(parser_e_feature_unsupported_for_vm);
  2723. resultdef:=voidtype;
  2724. { now we know the type of buffer }
  2725. hp:=ccallparanode.create(cordconstnode.create(
  2726. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2727. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2728. left:=nil;
  2729. end;
  2730. { the firstpass of the arg has been done in firstcalln ? }
  2731. in_reset_typedfile,
  2732. in_rewrite_typedfile :
  2733. begin
  2734. result := handle_reset_rewrite_typed;
  2735. end;
  2736. in_str_x_string :
  2737. begin
  2738. result:=handle_str;
  2739. end;
  2740. in_val_x :
  2741. begin
  2742. result:=handle_val;
  2743. end;
  2744. in_include_x_y,
  2745. in_exclude_x_y:
  2746. begin
  2747. resultdef:=voidtype;
  2748. { the parser already checks whether we have two (and exactly two) }
  2749. { parameters (JM) }
  2750. { first param must be var }
  2751. valid_for_var(tcallparanode(left).left,true);
  2752. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2753. { check type }
  2754. if (left.resultdef.typ=setdef) then
  2755. begin
  2756. { insert a type conversion }
  2757. { to the type of the set elements }
  2758. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2759. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2760. tsetdef(left.resultdef).elementdef);
  2761. end
  2762. else
  2763. CGMessage(type_e_mismatch);
  2764. end;
  2765. in_pack_x_y_z,
  2766. in_unpack_x_y_z :
  2767. begin
  2768. handle_pack_unpack;
  2769. end;
  2770. in_slice_x:
  2771. begin
  2772. if target_info.system in systems_managed_vm then
  2773. message(parser_e_feature_unsupported_for_vm);
  2774. result:=nil;
  2775. resultdef:=tcallparanode(left).left.resultdef;
  2776. if (resultdef.typ <> arraydef) then
  2777. CGMessagePos(left.fileinfo,type_e_mismatch)
  2778. else if is_packed_array(resultdef) then
  2779. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2780. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2781. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2782. type_e_integer_expr_expected,
  2783. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2784. end;
  2785. in_new_x:
  2786. resultdef:=left.resultdef;
  2787. in_low_x,
  2788. in_high_x:
  2789. begin
  2790. case left.resultdef.typ of
  2791. orddef,
  2792. enumdef,
  2793. setdef:
  2794. ;
  2795. arraydef:
  2796. begin
  2797. if (inlinenumber=in_low_x) then
  2798. set_varstate(left,vs_read,[])
  2799. else
  2800. begin
  2801. if is_open_array(left.resultdef) or
  2802. is_array_of_const(left.resultdef) then
  2803. begin
  2804. set_varstate(left,vs_read,[]);
  2805. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2806. end
  2807. else
  2808. if is_dynamic_array(left.resultdef) then
  2809. begin
  2810. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2811. { can't use inserttypeconv because we need }
  2812. { an explicit type conversion (JM) }
  2813. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2814. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2815. { make sure the left node doesn't get disposed, since it's }
  2816. { reused in the new node (JM) }
  2817. left:=nil;
  2818. end
  2819. else
  2820. begin
  2821. set_varstate(left,vs_read,[]);
  2822. end;
  2823. end;
  2824. end;
  2825. stringdef:
  2826. begin
  2827. if inlinenumber=in_low_x then
  2828. begin
  2829. set_varstate(left,vs_read,[]);
  2830. end
  2831. else
  2832. begin
  2833. if is_open_string(left.resultdef) then
  2834. begin
  2835. set_varstate(left,vs_read,[]);
  2836. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2837. end
  2838. else if is_dynamicstring(left.resultdef) then
  2839. begin
  2840. result:=cinlinenode.create(in_length_x,false,left);
  2841. if cs_zerobasedstrings in current_settings.localswitches then
  2842. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  2843. { make sure the left node doesn't get disposed, since it's }
  2844. { reused in the new node (JM) }
  2845. left:=nil;
  2846. end
  2847. end;
  2848. end;
  2849. else
  2850. CGMessage(type_e_mismatch);
  2851. end;
  2852. end;
  2853. in_exp_real,
  2854. in_frac_real,
  2855. in_int_real,
  2856. in_cos_real,
  2857. in_sin_real,
  2858. in_arctan_real,
  2859. in_abs_real,
  2860. in_ln_real :
  2861. begin
  2862. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2863. { converting an int64 to double on platforms without }
  2864. { extended can cause precision loss }
  2865. if not(left.nodetype in [ordconstn,realconstn]) then
  2866. inserttypeconv(left,pbestrealtype^);
  2867. resultdef:=pbestrealtype^;
  2868. end;
  2869. in_trunc_real,
  2870. in_round_real :
  2871. begin
  2872. { on i8086, the int64 result is returned in a var param, because
  2873. it's too big to fit in a register or a pair of registers. In
  2874. that case we have 2 parameters and left.nodetype is a callparan. }
  2875. if left.nodetype = callparan then
  2876. temp_pnode := @tcallparanode(left).left
  2877. else
  2878. temp_pnode := @left;
  2879. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  2880. { for direct float rounding, no best real type cast should be necessary }
  2881. if not((temp_pnode^.resultdef.typ=floatdef) and
  2882. (tfloatdef(temp_pnode^.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2883. { converting an int64 to double on platforms without }
  2884. { extended can cause precision loss }
  2885. not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  2886. inserttypeconv(temp_pnode^,pbestrealtype^);
  2887. resultdef:=s64inttype;
  2888. end;
  2889. in_pi_real :
  2890. begin
  2891. resultdef:=pbestrealtype^;
  2892. end;
  2893. in_abs_long:
  2894. begin
  2895. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2896. inserttypeconv(left,s32inttype);
  2897. resultdef:=s32inttype;
  2898. end;
  2899. in_sqr_real,
  2900. in_sqrt_real :
  2901. begin
  2902. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2903. setfloatresultdef;
  2904. end;
  2905. {$ifdef SUPPORT_MMX}
  2906. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2907. begin
  2908. end;
  2909. {$endif SUPPORT_MMX}
  2910. in_aligned_x,
  2911. in_unaligned_x:
  2912. begin
  2913. resultdef:=left.resultdef;
  2914. end;
  2915. in_assert_x_y :
  2916. begin
  2917. resultdef:=voidtype;
  2918. if assigned(left) then
  2919. begin
  2920. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2921. { check type }
  2922. if is_boolean(left.resultdef) then
  2923. begin
  2924. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2925. { must always be a string }
  2926. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2927. end
  2928. else
  2929. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2930. end
  2931. else
  2932. CGMessage(type_e_mismatch);
  2933. if (cs_do_assertion in current_settings.localswitches) then
  2934. include(current_procinfo.flags,pi_do_call);
  2935. end;
  2936. in_prefetch_var:
  2937. resultdef:=voidtype;
  2938. in_get_frame,
  2939. in_get_caller_frame,
  2940. in_get_caller_addr:
  2941. begin
  2942. resultdef:=voidpointertype;
  2943. end;
  2944. in_rol_x,
  2945. in_ror_x,
  2946. in_sar_x:
  2947. begin
  2948. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2949. resultdef:=left.resultdef;
  2950. end;
  2951. in_rol_x_y,
  2952. in_ror_x_y,
  2953. in_sar_x_y:
  2954. begin
  2955. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2956. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2957. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2958. end;
  2959. in_bsf_x,
  2960. in_bsr_x:
  2961. begin
  2962. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2963. if not is_integer(left.resultdef) then
  2964. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2965. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2966. resultdef:=u64inttype
  2967. else
  2968. resultdef:=u32inttype
  2969. end;
  2970. in_popcnt_x:
  2971. begin
  2972. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2973. if not is_integer(left.resultdef) then
  2974. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2975. resultdef:=left.resultdef;
  2976. end;
  2977. in_objc_selector_x:
  2978. begin
  2979. result:=cobjcselectornode.create(left);
  2980. { reused }
  2981. left:=nil;
  2982. end;
  2983. in_objc_protocol_x:
  2984. begin
  2985. result:=cobjcprotocolnode.create(left);
  2986. { reused }
  2987. left:=nil;
  2988. end;
  2989. in_objc_encode_x:
  2990. begin
  2991. result:=handle_objc_encode;
  2992. end;
  2993. in_default_x:
  2994. begin
  2995. result:=handle_default;
  2996. end;
  2997. in_box_x:
  2998. begin
  2999. result:=handle_box;
  3000. end;
  3001. in_unbox_x_y:
  3002. begin
  3003. result:=handle_unbox;
  3004. end;
  3005. else
  3006. internalerror(8);
  3007. end;
  3008. end;
  3009. if not assigned(result) and not
  3010. codegenerror then
  3011. result:=simplify(false);
  3012. end;
  3013. function tinlinenode.pass_1 : tnode;
  3014. var
  3015. hp,hpp,resultnode : tnode;
  3016. shiftconst: longint;
  3017. tempnode: ttempcreatenode;
  3018. newstatement: tstatementnode;
  3019. newblock: tblocknode;
  3020. begin
  3021. result:=nil;
  3022. { if we handle writeln; left contains no valid address }
  3023. if assigned(left) then
  3024. begin
  3025. if left.nodetype=callparan then
  3026. tcallparanode(left).firstcallparan
  3027. else
  3028. firstpass(left);
  3029. end;
  3030. { intern const should already be handled }
  3031. if nf_inlineconst in flags then
  3032. internalerror(200104044);
  3033. case inlinenumber of
  3034. in_lo_qword,
  3035. in_hi_qword,
  3036. in_lo_long,
  3037. in_hi_long,
  3038. in_lo_word,
  3039. in_hi_word:
  3040. begin
  3041. shiftconst := 0;
  3042. case inlinenumber of
  3043. in_hi_qword:
  3044. shiftconst := 32;
  3045. in_hi_long:
  3046. shiftconst := 16;
  3047. in_hi_word:
  3048. shiftconst := 8;
  3049. end;
  3050. if shiftconst <> 0 then
  3051. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  3052. cordconstnode.create(shiftconst,sinttype,false)),resultdef)
  3053. else
  3054. result := ctypeconvnode.create_internal(left,resultdef);
  3055. left := nil;
  3056. firstpass(result);
  3057. end;
  3058. in_sizeof_x,
  3059. in_typeof_x:
  3060. begin
  3061. expectloc:=LOC_REGISTER;
  3062. if (left.nodetype=typen) and
  3063. (cs_create_pic in current_settings.moduleswitches) and
  3064. (tf_pic_uses_got in target_info.flags) then
  3065. include(current_procinfo.flags,pi_needs_got);
  3066. end;
  3067. in_length_x:
  3068. begin
  3069. result:=first_length;
  3070. end;
  3071. in_typeinfo_x:
  3072. begin
  3073. result:=caddrnode.create_internal(
  3074. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  3075. );
  3076. end;
  3077. in_assigned_x:
  3078. begin
  3079. result:=first_assigned;
  3080. end;
  3081. in_pred_x,
  3082. in_succ_x:
  3083. begin
  3084. expectloc:=LOC_REGISTER;
  3085. { in case of range/overflow checking, use a regular addnode
  3086. because it's too complex to handle correctly otherwise }
  3087. {$ifndef jvm}
  3088. { enums are class instances in the JVM -> always need conversion }
  3089. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  3090. {$endif}
  3091. begin
  3092. { create constant 1 }
  3093. hp:=cordconstnode.create(1,left.resultdef,false);
  3094. typecheckpass(hp);
  3095. if not is_integer(hp.resultdef) then
  3096. inserttypeconv_internal(hp,sinttype);
  3097. { avoid type errors from the addn/subn }
  3098. if not is_integer(left.resultdef) then
  3099. inserttypeconv_internal(left,sinttype);
  3100. { addition/substraction depending on succ/pred }
  3101. if inlinenumber=in_succ_x then
  3102. hp:=caddnode.create(addn,left,hp)
  3103. else
  3104. hp:=caddnode.create(subn,left,hp);
  3105. { assign result of addition }
  3106. if not(is_integer(resultdef)) then
  3107. inserttypeconv(hp,torddef.create(
  3108. {$ifdef cpu64bitaddr}
  3109. s64bit,
  3110. {$else cpu64bitaddr}
  3111. s32bit,
  3112. {$endif cpu64bitaddr}
  3113. get_min_value(resultdef),
  3114. get_max_value(resultdef)))
  3115. else
  3116. inserttypeconv(hp,resultdef);
  3117. { avoid any possible errors/warnings }
  3118. inserttypeconv_internal(hp,resultdef);
  3119. { firstpass it }
  3120. firstpass(hp);
  3121. { left is reused }
  3122. left:=nil;
  3123. { return new node }
  3124. result:=hp;
  3125. end;
  3126. end;
  3127. in_setlength_x:
  3128. result:=first_setlength;
  3129. in_copy_x:
  3130. result:=first_copy;
  3131. in_initialize_x,
  3132. in_finalize_x:
  3133. begin
  3134. expectloc:=LOC_VOID;
  3135. end;
  3136. in_inc_x,
  3137. in_dec_x:
  3138. begin
  3139. expectloc:=LOC_VOID;
  3140. { range/overflow checking doesn't work properly }
  3141. { with the inc/dec code that's generated (JM) }
  3142. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3143. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3144. trigger an overflow. For uint32 it works because then the operation is done
  3145. in 64bit. Range checking is not applicable to pointers either }
  3146. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3147. {$ifdef jvm}
  3148. { enums are class instances on the JVM -> special treatment }
  3149. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3150. {$endif}
  3151. then
  3152. { convert to simple add (JM) }
  3153. begin
  3154. newblock := internalstatements(newstatement);
  3155. { extra parameter? }
  3156. if assigned(tcallparanode(left).right) then
  3157. begin
  3158. { Yes, use for add node }
  3159. hpp := tcallparanode(tcallparanode(left).right).left;
  3160. tcallparanode(tcallparanode(left).right).left := nil;
  3161. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3162. CGMessage(parser_e_illegal_expression);
  3163. end
  3164. else
  3165. begin
  3166. { no, create constant 1 }
  3167. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3168. end;
  3169. typecheckpass(hpp);
  3170. { make sure we don't call functions part of the left node twice (and generally }
  3171. { optimize the code generation) }
  3172. { Storing address is not always an optimization: alignment of left is not known
  3173. at this point, so we must assume the worst and use an unaligned pointer.
  3174. This results in larger and slower code on alignment-sensitive targets.
  3175. Therefore the complexity condition below is questionable, maybe just filtering
  3176. out calls with "= NODE_COMPLEXITY_INF" is sufficient.
  3177. Value of 3 corresponds to subscript nodes, i.e. record field. }
  3178. if node_complexity(tcallparanode(left).left) > 3 then
  3179. begin
  3180. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3181. addstatement(newstatement,tempnode);
  3182. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3183. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3184. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3185. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3186. end
  3187. else
  3188. begin
  3189. hp := tcallparanode(left).left.getcopy;
  3190. tempnode := nil;
  3191. end;
  3192. resultnode := hp.getcopy;
  3193. { avoid type errors from the addn/subn }
  3194. if not is_integer(resultnode.resultdef) then
  3195. begin
  3196. inserttypeconv_internal(hp,sinttype);
  3197. inserttypeconv_internal(hpp,sinttype);
  3198. end;
  3199. { addition/substraction depending on inc/dec }
  3200. if inlinenumber = in_inc_x then
  3201. hpp := caddnode.create(addn,hp,hpp)
  3202. else
  3203. hpp := caddnode.create(subn,hp,hpp);
  3204. { assign result of addition }
  3205. if not(is_integer(resultnode.resultdef)) then
  3206. inserttypeconv(hpp,torddef.create(
  3207. {$ifdef cpu64bitaddr}
  3208. s64bit,
  3209. {$else cpu64bitaddr}
  3210. s32bit,
  3211. {$endif cpu64bitaddr}
  3212. get_min_value(resultnode.resultdef),
  3213. get_max_value(resultnode.resultdef)))
  3214. else
  3215. inserttypeconv(hpp,resultnode.resultdef);
  3216. { avoid any possible warnings }
  3217. inserttypeconv_internal(hpp,resultnode.resultdef);
  3218. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3219. { deallocate the temp }
  3220. if assigned(tempnode) then
  3221. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3222. { firstpass it }
  3223. firstpass(tnode(newblock));
  3224. { return new node }
  3225. result := newblock;
  3226. end;
  3227. end;
  3228. in_include_x_y,
  3229. in_exclude_x_y:
  3230. begin
  3231. result:=first_IncludeExclude;
  3232. end;
  3233. in_pack_x_y_z,
  3234. in_unpack_x_y_z:
  3235. begin
  3236. result:=first_pack_unpack;
  3237. end;
  3238. in_exp_real:
  3239. begin
  3240. result:= first_exp_real;
  3241. end;
  3242. in_round_real:
  3243. begin
  3244. result:= first_round_real;
  3245. end;
  3246. in_trunc_real:
  3247. begin
  3248. result:= first_trunc_real;
  3249. end;
  3250. in_int_real:
  3251. begin
  3252. result:= first_int_real;
  3253. end;
  3254. in_frac_real:
  3255. begin
  3256. result:= first_frac_real;
  3257. end;
  3258. in_cos_real:
  3259. begin
  3260. result:= first_cos_real;
  3261. end;
  3262. in_sin_real:
  3263. begin
  3264. result := first_sin_real;
  3265. end;
  3266. in_arctan_real:
  3267. begin
  3268. result := first_arctan_real;
  3269. end;
  3270. in_pi_real :
  3271. begin
  3272. result := first_pi;
  3273. end;
  3274. in_abs_real:
  3275. begin
  3276. result := first_abs_real;
  3277. end;
  3278. in_abs_long:
  3279. begin
  3280. result := first_abs_long;
  3281. end;
  3282. in_sqr_real:
  3283. begin
  3284. result := first_sqr_real;
  3285. end;
  3286. in_sqrt_real:
  3287. begin
  3288. result := first_sqrt_real;
  3289. end;
  3290. in_ln_real:
  3291. begin
  3292. result := first_ln_real;
  3293. end;
  3294. {$ifdef SUPPORT_MMX}
  3295. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3296. begin
  3297. end;
  3298. {$endif SUPPORT_MMX}
  3299. in_assert_x_y :
  3300. begin
  3301. result:=first_assert;
  3302. end;
  3303. in_low_x,
  3304. in_high_x:
  3305. internalerror(200104047);
  3306. in_slice_x:
  3307. internalerror(2005101501);
  3308. in_ord_x,
  3309. in_chr_byte:
  3310. begin
  3311. { should not happend as it's converted to typeconv }
  3312. internalerror(200104045);
  3313. end;
  3314. in_ofs_x :
  3315. internalerror(2000101001);
  3316. in_seg_x :
  3317. begin
  3318. result:=first_seg;
  3319. end;
  3320. in_settextbuf_file_x,
  3321. in_reset_typedfile,
  3322. in_rewrite_typedfile,
  3323. in_str_x_string,
  3324. in_val_x,
  3325. in_read_x,
  3326. in_readln_x,
  3327. in_write_x,
  3328. in_writeln_x :
  3329. begin
  3330. { should be handled by pass_typecheck }
  3331. internalerror(200108234);
  3332. end;
  3333. in_get_frame:
  3334. begin
  3335. result:=first_get_frame;
  3336. end;
  3337. in_get_caller_frame:
  3338. begin
  3339. expectloc:=LOC_REGISTER;
  3340. end;
  3341. in_get_caller_addr:
  3342. begin
  3343. expectloc:=LOC_REGISTER;
  3344. end;
  3345. in_prefetch_var:
  3346. begin
  3347. expectloc:=LOC_VOID;
  3348. end;
  3349. in_aligned_x,
  3350. in_unaligned_x:
  3351. begin
  3352. expectloc:=tcallparanode(left).left.expectloc;
  3353. end;
  3354. in_rol_x,
  3355. in_rol_x_y,
  3356. in_ror_x,
  3357. in_ror_x_y,
  3358. in_bsf_x,
  3359. in_bsr_x:
  3360. expectloc:=LOC_REGISTER;
  3361. in_sar_x,
  3362. in_sar_x_y:
  3363. result:=first_sar;
  3364. in_popcnt_x:
  3365. result:=first_popcnt;
  3366. in_new_x:
  3367. result:=first_new;
  3368. in_box_x:
  3369. result:=first_box;
  3370. in_unbox_x_y:
  3371. result:=first_unbox;
  3372. else
  3373. internalerror(89);
  3374. end;
  3375. end;
  3376. {$maxfpuregisters default}
  3377. function tinlinenode.docompare(p: tnode): boolean;
  3378. begin
  3379. docompare :=
  3380. inherited docompare(p) and
  3381. (inlinenumber = tinlinenode(p).inlinenumber);
  3382. end;
  3383. function tinlinenode.first_pi : tnode;
  3384. begin
  3385. result:=crealconstnode.create(getpi,pbestrealtype^);
  3386. end;
  3387. function tinlinenode.first_arctan_real : tnode;
  3388. begin
  3389. { create the call to the helper }
  3390. { on entry left node contains the parameter }
  3391. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3392. ccallparanode.create(left,nil));
  3393. left := nil;
  3394. end;
  3395. function tinlinenode.first_abs_real : tnode;
  3396. begin
  3397. { create the call to the helper }
  3398. { on entry left node contains the parameter }
  3399. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3400. ccallparanode.create(left,nil));
  3401. left := nil;
  3402. end;
  3403. function tinlinenode.first_sqr_real : tnode;
  3404. begin
  3405. {$ifndef cpufpemu}
  3406. { this procedure might be only used for cpus definining cpufpemu else
  3407. the optimizer might go into an endless loop when doing x*x -> changes }
  3408. internalerror(2011092401);
  3409. {$endif cpufpemu}
  3410. { create the call to the helper }
  3411. { on entry left node contains the parameter }
  3412. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3413. ccallparanode.create(left,nil)),resultdef);
  3414. left := nil;
  3415. end;
  3416. function tinlinenode.first_sqrt_real : tnode;
  3417. begin
  3418. { create the call to the helper }
  3419. { on entry left node contains the parameter }
  3420. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3421. ccallparanode.create(left,nil)),resultdef);
  3422. left := nil;
  3423. end;
  3424. function tinlinenode.first_ln_real : tnode;
  3425. begin
  3426. { create the call to the helper }
  3427. { on entry left node contains the parameter }
  3428. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3429. ccallparanode.create(left,nil));
  3430. left := nil;
  3431. end;
  3432. function tinlinenode.first_cos_real : tnode;
  3433. begin
  3434. { create the call to the helper }
  3435. { on entry left node contains the parameter }
  3436. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3437. ccallparanode.create(left,nil));
  3438. left := nil;
  3439. end;
  3440. function tinlinenode.first_sin_real : tnode;
  3441. begin
  3442. { create the call to the helper }
  3443. { on entry left node contains the parameter }
  3444. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3445. ccallparanode.create(left,nil));
  3446. left := nil;
  3447. end;
  3448. function tinlinenode.first_exp_real : tnode;
  3449. begin
  3450. { create the call to the helper }
  3451. { on entry left node contains the parameter }
  3452. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3453. left := nil;
  3454. end;
  3455. function tinlinenode.first_int_real : tnode;
  3456. begin
  3457. { create the call to the helper }
  3458. { on entry left node contains the parameter }
  3459. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3460. left := nil;
  3461. end;
  3462. function tinlinenode.first_frac_real : tnode;
  3463. begin
  3464. { create the call to the helper }
  3465. { on entry left node contains the parameter }
  3466. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3467. left := nil;
  3468. end;
  3469. function tinlinenode.first_round_real : tnode;
  3470. begin
  3471. { create the call to the helper }
  3472. { on entry left node contains the parameter }
  3473. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3474. left := nil;
  3475. end;
  3476. function tinlinenode.first_trunc_real : tnode;
  3477. begin
  3478. { create the call to the helper }
  3479. { on entry left node contains the parameter }
  3480. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3481. left := nil;
  3482. end;
  3483. function tinlinenode.first_abs_long : tnode;
  3484. begin
  3485. expectloc:=LOC_REGISTER;
  3486. result:=nil;
  3487. end;
  3488. function tinlinenode.first_IncludeExclude: tnode;
  3489. begin
  3490. result:=nil;
  3491. expectloc:=LOC_VOID;
  3492. end;
  3493. function tinlinenode.first_get_frame: tnode;
  3494. begin
  3495. include(current_procinfo.flags,pi_needs_stackframe);
  3496. expectloc:=LOC_CREGISTER;
  3497. result:=nil;
  3498. end;
  3499. function tinlinenode.first_setlength: tnode;
  3500. var
  3501. paras : tnode;
  3502. npara,
  3503. ppn : tcallparanode;
  3504. dims,
  3505. counter : integer;
  3506. isarray : boolean;
  3507. destppn : tnode;
  3508. newstatement : tstatementnode;
  3509. temp : ttempcreatenode;
  3510. newblock : tnode;
  3511. begin
  3512. paras:=left;
  3513. ppn:=tcallparanode(paras);
  3514. dims:=0;
  3515. while assigned(ppn.right) do
  3516. begin
  3517. inc(dims);
  3518. ppn:=tcallparanode(ppn.right);
  3519. end;
  3520. destppn:=ppn.left;
  3521. isarray:=is_dynamic_array(destppn.resultdef);
  3522. { first param must be a string or dynamic array ...}
  3523. if isarray then
  3524. begin
  3525. { create statements with call initialize the arguments and
  3526. call fpc_dynarr_setlength }
  3527. newblock:=internalstatements(newstatement);
  3528. { get temp for array of lengths }
  3529. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3530. addstatement(newstatement,temp);
  3531. { load array of lengths }
  3532. ppn:=tcallparanode(paras);
  3533. counter:=dims-1;
  3534. while assigned(ppn.right) do
  3535. begin
  3536. addstatement(newstatement,cassignmentnode.create(
  3537. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3538. ppn.left));
  3539. ppn.left:=nil;
  3540. dec(counter);
  3541. ppn:=tcallparanode(ppn.right);
  3542. end;
  3543. { destppn is also reused }
  3544. ppn.left:=nil;
  3545. { create call to fpc_dynarr_setlength }
  3546. npara:=ccallparanode.create(caddrnode.create_internal
  3547. (ctemprefnode.create(temp)),
  3548. ccallparanode.create(cordconstnode.create
  3549. (dims,sinttype,true),
  3550. ccallparanode.create(caddrnode.create_internal
  3551. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3552. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3553. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3554. addstatement(newstatement,ctempdeletenode.create(temp));
  3555. end
  3556. else if is_ansistring(destppn.resultdef) then
  3557. begin
  3558. newblock:=ccallnode.createintern(
  3559. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3560. ccallparanode.create(
  3561. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3562. paras
  3563. )
  3564. );
  3565. { we reused the parameters, make sure we don't release them }
  3566. left:=nil;
  3567. end
  3568. else
  3569. begin
  3570. { we can reuse the supplied parameters }
  3571. newblock:=ccallnode.createintern(
  3572. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3573. { we reused the parameters, make sure we don't release them }
  3574. left:=nil;
  3575. end;
  3576. result:=newblock;
  3577. end;
  3578. function tinlinenode.first_copy: tnode;
  3579. var
  3580. lowppn,
  3581. highppn,
  3582. npara,
  3583. paras : tnode;
  3584. ppn : tcallparanode;
  3585. paradef : tdef;
  3586. counter : integer;
  3587. begin
  3588. { determine copy function to use based on the first argument,
  3589. also count the number of arguments in this loop }
  3590. counter:=1;
  3591. paras:=left;
  3592. ppn:=tcallparanode(paras);
  3593. while assigned(ppn.right) do
  3594. begin
  3595. inc(counter);
  3596. ppn:=tcallparanode(ppn.right);
  3597. end;
  3598. paradef:=ppn.left.resultdef;
  3599. { fill up third parameter }
  3600. if counter=2 then
  3601. begin
  3602. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3603. counter:=3;
  3604. end;
  3605. if is_ansistring(resultdef) then
  3606. { keep the specific kind of ansistringdef as result }
  3607. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3608. else if is_widestring(resultdef) then
  3609. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3610. else if is_unicodestring(resultdef) then
  3611. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3612. { can't check for resultdef = cansichartype, because resultdef=
  3613. cshortstringtype here }
  3614. else if is_char(paradef) then
  3615. result:=ccallnode.createintern('fpc_char_copy',paras)
  3616. else if is_dynamic_array(resultdef) then
  3617. begin
  3618. { create statements with call }
  3619. case counter of
  3620. 1:
  3621. begin
  3622. { copy the whole array using [0..high(sizeint)] range }
  3623. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3624. lowppn:=cordconstnode.create(0,sinttype,false);
  3625. end;
  3626. 3:
  3627. begin
  3628. highppn:=tcallparanode(paras).left.getcopy;
  3629. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3630. end;
  3631. else
  3632. internalerror(2012100701);
  3633. end;
  3634. { create call to fpc_dynarray_copy }
  3635. npara:=ccallparanode.create(highppn,
  3636. ccallparanode.create(lowppn,
  3637. ccallparanode.create(caddrnode.create_internal
  3638. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3639. ccallparanode.create
  3640. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3641. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3642. ppn.left:=nil;
  3643. paras.free;
  3644. end
  3645. else
  3646. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3647. { parameters are reused }
  3648. left:=nil;
  3649. end;
  3650. function tinlinenode.first_new: tnode;
  3651. var
  3652. newstatement : tstatementnode;
  3653. newblock : tblocknode;
  3654. temp : ttempcreatenode;
  3655. para : tcallparanode;
  3656. begin
  3657. { create statements with call to getmem+initialize }
  3658. newblock:=internalstatements(newstatement);
  3659. { create temp for result }
  3660. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  3661. addstatement(newstatement,temp);
  3662. { create call to fpc_getmem }
  3663. para := ccallparanode.create(cordconstnode.create
  3664. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  3665. addstatement(newstatement,cassignmentnode.create(
  3666. ctemprefnode.create(temp),
  3667. ccallnode.createintern('fpc_getmem',para)));
  3668. { create call to fpc_initialize }
  3669. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  3670. begin
  3671. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  3672. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  3673. ccallparanode.create(ctemprefnode.create
  3674. (temp),nil));
  3675. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  3676. end;
  3677. { the last statement should return the value as
  3678. location and type, this is done be referencing the
  3679. temp and converting it first from a persistent temp to
  3680. normal temp }
  3681. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3682. addstatement(newstatement,ctemprefnode.create(temp));
  3683. result:=newblock;
  3684. end;
  3685. function tinlinenode.first_length: tnode;
  3686. begin
  3687. result:=nil;
  3688. if is_shortstring(left.resultdef) then
  3689. expectloc:=left.expectloc
  3690. else
  3691. begin
  3692. { ansi/wide string }
  3693. expectloc:=LOC_REGISTER;
  3694. end;
  3695. end;
  3696. function tinlinenode.first_assigned: tnode;
  3697. begin
  3698. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  3699. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  3700. include(result.flags,nf_load_procvar);
  3701. tcallparanode(left).left:=nil;
  3702. end;
  3703. function tinlinenode.first_assert: tnode;
  3704. var
  3705. paras: tcallparanode;
  3706. begin
  3707. paras:=tcallparanode(tcallparanode(left).right);
  3708. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3709. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3710. {$ifdef SUPPORT_GET_FRAME}
  3711. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3712. {$else}
  3713. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3714. {$endif}
  3715. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3716. ccallnode.createintern('fpc_assert',paras),nil);
  3717. tcallparanode(left).left:=nil;
  3718. tcallparanode(left).right:=nil;
  3719. end;
  3720. function tinlinenode.first_popcnt: tnode;
  3721. var
  3722. suffix : string;
  3723. begin
  3724. case torddef(left.resultdef).ordtype of
  3725. u8bit: suffix:='byte';
  3726. u16bit: suffix:='word';
  3727. u32bit: suffix:='dword';
  3728. u64bit: suffix:='qword';
  3729. else
  3730. internalerror(2012082601);
  3731. end;
  3732. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  3733. left:=nil;
  3734. end;
  3735. function tinlinenode.typecheck_seg: tnode;
  3736. begin
  3737. if target_info.system in systems_managed_vm then
  3738. message(parser_e_feature_unsupported_for_vm);
  3739. set_varstate(left,vs_read,[]);
  3740. result:=cordconstnode.create(0,s32inttype,false);
  3741. end;
  3742. function tinlinenode.first_seg: tnode;
  3743. begin
  3744. internalerror(200104046);
  3745. result:=nil;
  3746. end;
  3747. function tinlinenode.first_sar: tnode;
  3748. begin
  3749. result:=nil;
  3750. expectloc:=LOC_REGISTER;
  3751. {$ifndef cpu64bitalu}
  3752. if is_64bitint(resultdef) then
  3753. begin
  3754. if (inlinenumber=in_sar_x) then
  3755. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  3756. ccallparanode.create(left,nil));
  3757. result:=ccallnode.createintern('fpc_sarint64',left);
  3758. left:=nil;
  3759. end;
  3760. {$endif cpu64bitalu}
  3761. end;
  3762. function tinlinenode.handle_box: tnode;
  3763. begin
  3764. result:=nil;
  3765. if not assigned(left) or
  3766. assigned(tcallparanode(left).right) then
  3767. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3768. resultdef:=class_tobject;
  3769. end;
  3770. function tinlinenode.handle_unbox: tnode;
  3771. begin
  3772. result:=nil;
  3773. if not assigned(left) or
  3774. not assigned(tcallparanode(left).right) or
  3775. assigned(tcallparanode(tcallparanode(left).right).right) then
  3776. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3777. if tcallparanode(left).left.nodetype<>typen then
  3778. internalerror(2011071701);
  3779. ttypenode(tcallparanode(left).left).allowed:=true;
  3780. resultdef:=tcallparanode(left).left.resultdef;
  3781. end;
  3782. function tinlinenode.first_pack_unpack: tnode;
  3783. var
  3784. loopstatement : tstatementnode;
  3785. loop : tblocknode;
  3786. loopvar : ttempcreatenode;
  3787. tempnode,
  3788. source,
  3789. target,
  3790. index,
  3791. unpackednode,
  3792. packednode,
  3793. sourcevecindex,
  3794. targetvecindex,
  3795. loopbody : tnode;
  3796. temprangedef : tdef;
  3797. ulorange,
  3798. uhirange,
  3799. plorange,
  3800. phirange : TConstExprInt;
  3801. begin
  3802. { transform into a for loop which assigns the data of the (un)packed }
  3803. { array to the other one }
  3804. source := left;
  3805. if (inlinenumber = in_unpack_x_y_z) then
  3806. begin
  3807. target := tcallparanode(source).right;
  3808. index := tcallparanode(target).right;
  3809. packednode := tcallparanode(source).left;
  3810. unpackednode := tcallparanode(target).left;
  3811. end
  3812. else
  3813. begin
  3814. index := tcallparanode(source).right;
  3815. target := tcallparanode(index).right;
  3816. packednode := tcallparanode(target).left;
  3817. unpackednode := tcallparanode(source).left;
  3818. end;
  3819. source := tcallparanode(source).left;
  3820. target := tcallparanode(target).left;
  3821. index := tcallparanode(index).left;
  3822. loop := internalstatements(loopstatement);
  3823. loopvar := ctempcreatenode.create(
  3824. tarraydef(packednode.resultdef).rangedef,
  3825. tarraydef(packednode.resultdef).rangedef.size,
  3826. tt_persistent,true);
  3827. addstatement(loopstatement,loopvar);
  3828. { For range checking: we have to convert to an integer type (in case the index type }
  3829. { is an enum), add the index and loop variable together, convert the result }
  3830. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3831. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3832. { errors }
  3833. temprangedef:=nil;
  3834. getrange(unpackednode.resultdef,ulorange,uhirange);
  3835. getrange(packednode.resultdef,plorange,phirange);
  3836. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3837. sourcevecindex := ctemprefnode.create(loopvar);
  3838. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3839. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3840. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3841. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3842. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3843. if (inlinenumber = in_pack_x_y_z) then
  3844. begin
  3845. { swap source and target vec indices }
  3846. tempnode := sourcevecindex;
  3847. sourcevecindex := targetvecindex;
  3848. targetvecindex := tempnode;
  3849. end;
  3850. { create the assignment in the loop body }
  3851. loopbody :=
  3852. cassignmentnode.create(
  3853. cvecnode.create(target.getcopy,targetvecindex),
  3854. cvecnode.create(source.getcopy,sourcevecindex)
  3855. );
  3856. { create the actual for loop }
  3857. tempnode := cfornode.create(
  3858. ctemprefnode.create(loopvar),
  3859. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3860. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3861. loopbody,
  3862. false);
  3863. addstatement(loopstatement,tempnode);
  3864. { free the loop counter }
  3865. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3866. result := loop;
  3867. end;
  3868. end.