ninl.pas 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190
  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] of string[15]=('fpc_typed_write','fpc_typed_read');
  960. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  961. var found_error,do_read,is_rwstr:boolean;
  962. para,nextpara:Tcallparanode;
  963. p1:Tnode;
  964. temp:Ttempcreatenode;
  965. begin
  966. found_error:=false;
  967. para:=Tcallparanode(params);
  968. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  969. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  970. temp:=nil;
  971. { add the typesize to the filepara }
  972. if filepara.resultdef.typ=filedef then
  973. filepara.right := ccallparanode.create(cordconstnode.create(
  974. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  975. { check for "no parameters" (you need at least one extra para for typed files) }
  976. if not assigned(para) then
  977. begin
  978. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  979. found_error := true;
  980. end;
  981. { process all parameters }
  982. while assigned(para) do
  983. begin
  984. { check if valid parameter }
  985. if para.left.nodetype=typen then
  986. begin
  987. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  988. found_error := true;
  989. end;
  990. { support writeln(procvar) }
  991. if (para.left.resultdef.typ=procvardef) then
  992. begin
  993. p1:=ccallnode.create_procvar(nil,para.left);
  994. typecheckpass(p1);
  995. para.left:=p1;
  996. end;
  997. if filepara.resultdef.typ=filedef then
  998. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  999. if assigned(para.right) and
  1000. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  1001. begin
  1002. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  1003. { skip all colon para's }
  1004. nextpara := tcallparanode(tcallparanode(para.right).right);
  1005. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  1006. nextpara := tcallparanode(nextpara.right);
  1007. found_error := true;
  1008. end
  1009. else
  1010. { get next parameter }
  1011. nextpara := tcallparanode(para.right);
  1012. { When we have a call, we have a problem: you can't pass the }
  1013. { result of a call as a formal const parameter. Solution: }
  1014. { assign the result to a temp and pass this temp as parameter }
  1015. { This is not very efficient, but write(typedfile,x) is }
  1016. { already slow by itself anyway (no buffering) (JM) }
  1017. { Actually, thge same goes for every non-simple expression }
  1018. { (such as an addition, ...) -> put everything but load nodes }
  1019. { into temps (JM) }
  1020. { of course, this must only be allowed for writes!!! (JM) }
  1021. if not(do_read) and (para.left.nodetype <> loadn) then
  1022. begin
  1023. { create temp for result }
  1024. temp := ctempcreatenode.create(para.left.resultdef,
  1025. para.left.resultdef.size,tt_persistent,false);
  1026. addstatement(Tstatementnode(newstatement),temp);
  1027. { assign result to temp }
  1028. addstatement(Tstatementnode(newstatement),
  1029. cassignmentnode.create(ctemprefnode.create(temp),
  1030. para.left));
  1031. { replace (reused) paranode with temp }
  1032. para.left := ctemprefnode.create(temp);
  1033. end;
  1034. { add fileparameter }
  1035. para.right := filepara.getcopy;
  1036. { create call statment }
  1037. { since the parameters are in the correct order, we have to insert }
  1038. { the statements always at the end of the current block }
  1039. addstatement(Tstatementnode(newstatement),
  1040. Ccallnode.createintern(procprefixes[do_read],para
  1041. ));
  1042. { if we used a temp, free it }
  1043. if para.left.nodetype = temprefn then
  1044. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1045. { process next parameter }
  1046. para := nextpara;
  1047. end;
  1048. { free the file parameter }
  1049. filepara.free;
  1050. handle_typed_read_write:=found_error;
  1051. end;
  1052. function tinlinenode.handle_read_write: tnode;
  1053. var
  1054. filepara,
  1055. nextpara,
  1056. params : tcallparanode;
  1057. newstatement : tstatementnode;
  1058. newblock : tblocknode;
  1059. filetemp : Ttempcreatenode;
  1060. name : string[31];
  1061. textsym : ttypesym;
  1062. is_typed,
  1063. do_read,
  1064. is_rwstr,
  1065. found_error : boolean;
  1066. begin
  1067. filepara := nil;
  1068. is_typed := false;
  1069. filetemp := nil;
  1070. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1071. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1072. { if we fail, we can quickly exit this way. We must generate something }
  1073. { instead of the inline node, because firstpass will bomb with an }
  1074. { internalerror if it encounters a read/write }
  1075. result := cerrornode.create;
  1076. { reverse the parameters (needed to get the colon parameters in the }
  1077. { correct order when processing write(ln) }
  1078. left := reverseparameters(tcallparanode(left));
  1079. if is_rwstr then
  1080. begin
  1081. filepara := tcallparanode(left);
  1082. { needs at least two parameters: source/dest string + min. 1 value }
  1083. if not(assigned(filepara)) or
  1084. not(assigned(filepara.right)) then
  1085. begin
  1086. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  1087. exit;
  1088. end
  1089. else if (filepara.resultdef.typ <> stringdef) then
  1090. begin
  1091. { convert chararray to string, or give an appropriate error message }
  1092. { (if you want to optimize to use shortstring, keep in mind that }
  1093. { readstr internally always uses ansistring, and to account for }
  1094. { chararrays with > 255 characters) }
  1095. inserttypeconv(filepara.left,getansistringdef);
  1096. filepara.resultdef:=filepara.left.resultdef;
  1097. if codegenerror then
  1098. exit;
  1099. end
  1100. end
  1101. else if assigned(left) then
  1102. begin
  1103. { check if we have a file parameter and if yes, what kind it is }
  1104. filepara := tcallparanode(left);
  1105. if (filepara.resultdef.typ=filedef) then
  1106. begin
  1107. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  1108. begin
  1109. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  1110. exit;
  1111. end
  1112. else
  1113. begin
  1114. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  1115. begin
  1116. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  1117. begin
  1118. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  1119. exit;
  1120. end;
  1121. is_typed := true;
  1122. end
  1123. end;
  1124. end
  1125. else
  1126. filepara := nil;
  1127. end;
  1128. { create a blocknode in which the successive write/read statements will be }
  1129. { put, since they belong together. Also create a dummy statement already to }
  1130. { make inserting of additional statements easier }
  1131. newblock:=internalstatements(newstatement);
  1132. if is_rwstr then
  1133. begin
  1134. { create a dummy temp text file that will be used to cache the
  1135. readstr/writestr state. Can't use a global variable in the system
  1136. unit because these can be nested (in case of parameters to
  1137. writestr that are function calls to functions that also call
  1138. readstr/writestr) }
  1139. textsym:=search_system_type('TEXT');
  1140. filetemp:=ctempcreatenode.create(textsym.typedef,textsym.typedef.size,tt_persistent,false);
  1141. addstatement(newstatement,filetemp);
  1142. if (do_read) then
  1143. name:='fpc_setupreadstr_'
  1144. else
  1145. name:='fpc_setupwritestr_';
  1146. name:=name+tstringdef(filepara.resultdef).stringtypname;
  1147. { the file para is a var parameter, but it is properly initialized,
  1148. so it should be actually an out parameter }
  1149. if not(do_read) then
  1150. set_varstate(filepara.left,vs_written,[]);
  1151. { remove the source/destination string parameter from the }
  1152. { parameter chain }
  1153. left:=filepara.right;
  1154. filepara.right:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1155. { pass the temp text file and the source/destination string to the
  1156. setup routine, which will store the string's address in the
  1157. textrec }
  1158. addstatement(newstatement,ccallnode.createintern(name,filepara));
  1159. filepara:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1160. end
  1161. { if we don't have a filepara, create one containing the default }
  1162. else if not assigned(filepara) then
  1163. begin
  1164. { since the input/output variables are threadvars loading them into
  1165. a temp once is faster. Create a temp which will hold a pointer to the file }
  1166. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1167. addstatement(newstatement,filetemp);
  1168. { make sure the resultdef of the temp (and as such of the }
  1169. { temprefs coming after it) is set (necessary because the }
  1170. { temprefs will be part of the filepara, of which we need }
  1171. { the resultdef later on and temprefs can only be }
  1172. { typecheckpassed if the resultdef of the temp is known) }
  1173. typecheckpass(tnode(filetemp));
  1174. { assign the address of the file to the temp }
  1175. if do_read then
  1176. name := 'input'
  1177. else
  1178. name := 'output';
  1179. addstatement(newstatement,
  1180. cassignmentnode.create(ctemprefnode.create(filetemp),
  1181. ccallnode.createintern('fpc_get_'+name,nil)));
  1182. { create a new fileparameter as follows: file_type(temp^) }
  1183. { (so that we pass the value and not the address of the temp }
  1184. { to the read/write routine) }
  1185. textsym:=search_system_type('TEXT');
  1186. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  1187. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  1188. end
  1189. else
  1190. { remove filepara from the parameter chain }
  1191. begin
  1192. left := filepara.right;
  1193. filepara.right := nil;
  1194. { the file para is a var parameter, but it must be valid already }
  1195. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  1196. { check if we should make a temp to store the result of a complex }
  1197. { expression (better heuristics, anyone?) (JM) }
  1198. if (filepara.left.nodetype <> loadn) then
  1199. begin
  1200. { create a temp which will hold a pointer to the file }
  1201. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1202. { add it to the statements }
  1203. addstatement(newstatement,filetemp);
  1204. { make sure the resultdef of the temp (and as such of the }
  1205. { temprefs coming after it) is set (necessary because the }
  1206. { temprefs will be part of the filepara, of which we need }
  1207. { the resultdef later on and temprefs can only be }
  1208. { typecheckpassed if the resultdef of the temp is known) }
  1209. typecheckpass(tnode(filetemp));
  1210. { assign the address of the file to the temp }
  1211. addstatement(newstatement,
  1212. cassignmentnode.create(ctemprefnode.create(filetemp),
  1213. caddrnode.create_internal(filepara.left)));
  1214. typecheckpass(newstatement.left);
  1215. { create a new fileparameter as follows: file_type(temp^) }
  1216. { (so that we pass the value and not the address of the temp }
  1217. { to the read/write routine) }
  1218. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  1219. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  1220. { replace the old file para with the new one }
  1221. filepara.left := nil;
  1222. filepara.free;
  1223. filepara := nextpara;
  1224. end;
  1225. end;
  1226. { the resultdef of the filepara must be set since it's }
  1227. { used below }
  1228. filepara.get_paratype;
  1229. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  1230. { if something goes wrong or at the end of the procedure }
  1231. { we're going to reuse the paranodes, so make sure they don't get freed }
  1232. { twice }
  1233. params:=Tcallparanode(left);
  1234. left := nil;
  1235. if is_typed then
  1236. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),tnode(newstatement))
  1237. else
  1238. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),tnode(newstatement));
  1239. { if we found an error, simply delete the generated blocknode }
  1240. if found_error then
  1241. newblock.free
  1242. else
  1243. begin
  1244. { deallocate the temp for the file para if we used one }
  1245. if assigned(filetemp) then
  1246. addstatement(newstatement,ctempdeletenode.create(filetemp));
  1247. { otherwise return the newly generated block of instructions, }
  1248. { but first free the errornode we generated at the beginning }
  1249. result.free;
  1250. result := newblock
  1251. end;
  1252. end;
  1253. function get_val_int_func(def: tdef): string;
  1254. var
  1255. ordtype: tordtype;
  1256. begin
  1257. ordtype := torddef(def).ordtype;
  1258. if not (ordtype in [s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  1259. internalerror(2013032603);
  1260. if is_oversizedint(def) then
  1261. begin
  1262. case ordtype of
  1263. s64bit: exit('int64');
  1264. u64bit: exit('qword');
  1265. s32bit: exit('longint');
  1266. u32bit: exit('longword');
  1267. s16bit: exit('smallint');
  1268. u16bit: exit('word');
  1269. else
  1270. internalerror(2013032604);
  1271. end;
  1272. end
  1273. else
  1274. begin
  1275. case ordtype of
  1276. s64bit,s32bit,s16bit,s8bit: exit('sint');
  1277. u64bit,u32bit,u16bit,u8bit: exit('uint');
  1278. else
  1279. internalerror(2013032604);
  1280. end;
  1281. end;
  1282. internalerror(2013032605);
  1283. end;
  1284. function tinlinenode.handle_val: tnode;
  1285. var
  1286. procname,
  1287. suffix : string[31];
  1288. sourcepara,
  1289. destpara,
  1290. codepara,
  1291. sizepara,
  1292. newparas : tcallparanode;
  1293. orgcode,tc : tnode;
  1294. newstatement : tstatementnode;
  1295. newblock : tblocknode;
  1296. tempcode : ttempcreatenode;
  1297. begin
  1298. { for easy exiting if something goes wrong }
  1299. result := cerrornode.create;
  1300. { check the amount of parameters }
  1301. if not(assigned(left)) or
  1302. not(assigned(tcallparanode(left).right)) then
  1303. begin
  1304. CGMessage1(parser_e_wrong_parameter_size,'Val');
  1305. exit;
  1306. end;
  1307. suffix:='';
  1308. { in case we are in a generic definition, we cannot
  1309. do all checks, the parameters might be type parameters }
  1310. if df_generic in current_procinfo.procdef.defoptions then
  1311. begin
  1312. result.Free;
  1313. result:=nil;
  1314. resultdef:=voidtype;
  1315. exit;
  1316. end;
  1317. { reverse parameters for easier processing }
  1318. left := reverseparameters(tcallparanode(left));
  1319. { get the parameters }
  1320. tempcode := nil;
  1321. orgcode := nil;
  1322. sizepara := nil;
  1323. sourcepara := tcallparanode(left);
  1324. destpara := tcallparanode(sourcepara.right);
  1325. codepara := tcallparanode(destpara.right);
  1326. { check if codepara is valid }
  1327. if assigned(codepara) and
  1328. (
  1329. not is_integer(codepara.resultdef)
  1330. {$ifndef cpu64bitaddr}
  1331. or is_64bitint(codepara.resultdef)
  1332. {$endif not cpu64bitaddr}
  1333. ) then
  1334. begin
  1335. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1336. exit;
  1337. end;
  1338. { check if dest para is valid }
  1339. if not is_integer(destpara.resultdef) and
  1340. not is_currency(destpara.resultdef) and
  1341. not(destpara.resultdef.typ in [floatdef,enumdef]) then
  1342. begin
  1343. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1344. exit;
  1345. end;
  1346. { we're going to reuse the exisiting para's, so make sure they }
  1347. { won't be disposed }
  1348. left := nil;
  1349. { create the blocknode which will hold the generated statements + }
  1350. { an initial dummy statement }
  1351. newblock:=internalstatements(newstatement);
  1352. { do we need a temp for code? Yes, if no code specified, or if }
  1353. { code is not a 32bit parameter (we already checked whether the }
  1354. { the code para, if specified, was an orddef) }
  1355. if not assigned(codepara) or
  1356. (codepara.resultdef.size<>ptrsinttype.size) then
  1357. begin
  1358. tempcode := ctempcreatenode.create(ptrsinttype,ptrsinttype.size,tt_persistent,false);
  1359. addstatement(newstatement,tempcode);
  1360. { set the resultdef of the temp (needed to be able to get }
  1361. { the resultdef of the tempref used in the new code para) }
  1362. typecheckpass(tnode(tempcode));
  1363. { create a temp codepara, but save the original code para to }
  1364. { assign the result to later on }
  1365. if assigned(codepara) then
  1366. begin
  1367. orgcode := codepara.left;
  1368. codepara.left := ctemprefnode.create(tempcode);
  1369. end
  1370. else
  1371. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1372. { we need its resultdef later on }
  1373. codepara.get_paratype;
  1374. end
  1375. else if (torddef(codepara.resultdef).ordtype <> torddef(ptrsinttype).ordtype) then
  1376. { because code is a var parameter, it must match types exactly }
  1377. { however, since it will return values >= 0, both signed and }
  1378. { and unsigned ints of the same size are fine. Since the formal }
  1379. { code para type is sinttype, insert a typecoversion to sint for }
  1380. { unsigned para's }
  1381. begin
  1382. codepara.left := ctypeconvnode.create_internal(codepara.left,ptrsinttype);
  1383. { make it explicit, oterwise you may get a nonsense range }
  1384. { check error if the cardinal already contained a value }
  1385. { > $7fffffff }
  1386. codepara.get_paratype;
  1387. end;
  1388. { create the procedure name }
  1389. procname := 'fpc_val_';
  1390. case destpara.resultdef.typ of
  1391. orddef:
  1392. begin
  1393. case torddef(destpara.resultdef).ordtype of
  1394. s8bit,s16bit,s32bit,s64bit,
  1395. u8bit,u16bit,u32bit,u64bit:
  1396. begin
  1397. suffix := get_val_int_func(destpara.resultdef) + '_';
  1398. { we also need a destsize para in the case of sint }
  1399. if suffix = 'sint_' then
  1400. sizepara := ccallparanode.create(cordconstnode.create
  1401. (destpara.resultdef.size,s32inttype,true),nil);
  1402. end;
  1403. scurrency: suffix := 'currency_';
  1404. else
  1405. internalerror(200304225);
  1406. end;
  1407. end;
  1408. floatdef:
  1409. suffix:='real_';
  1410. enumdef:
  1411. begin
  1412. suffix:='enum_';
  1413. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1414. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1415. ),nil);
  1416. end;
  1417. end;
  1418. procname := procname + suffix;
  1419. { play a trick to have tcallnode handle invalid source parameters: }
  1420. { the shortstring-longint val routine by default }
  1421. if (sourcepara.resultdef.typ = stringdef) then
  1422. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1423. { zero-based arrays (of char) can be implicitely converted to ansistring, but don't do
  1424. so if not needed because the array is too short }
  1425. else if is_zero_based_array(sourcepara.resultdef) and (sourcepara.resultdef.size>255) then
  1426. procname := procname + 'ansistr'
  1427. else
  1428. procname := procname + 'shortstr';
  1429. { set up the correct parameters for the call: the code para... }
  1430. newparas := codepara;
  1431. { and the source para }
  1432. codepara.right := sourcepara;
  1433. { sizepara either contains nil if none is needed (which is ok, since }
  1434. { then the next statement severes any possible links with other paras }
  1435. { that sourcepara may have) or it contains the necessary size para and }
  1436. { its right field is nil }
  1437. sourcepara.right := sizepara;
  1438. { create the call and assign the result to dest (val helpers are functions).
  1439. Use a trick to prevent a type size mismatch warning to be generated by the
  1440. assignment node. First convert implicitly to the resultdef. This will insert
  1441. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1442. for the assignment node and therefor preventing the warning (PFV)
  1443. The implicit conversion is avoided for enums because implicit conversion between
  1444. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1445. possible. (DM).
  1446. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1447. to prevent warning about automatic type conversion. }
  1448. if (destpara.resultdef.typ=enumdef) or
  1449. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1450. then
  1451. tc:=ccallnode.createintern(procname,newparas)
  1452. else
  1453. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1454. addstatement(newstatement,cassignmentnode.create(
  1455. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1456. { dispose of the enclosing paranode of the destination }
  1457. destpara.left := nil;
  1458. destpara.right := nil;
  1459. destpara.free;
  1460. { check if we used a temp for code and whether we have to store }
  1461. { it to the real code parameter }
  1462. if assigned(orgcode) then
  1463. addstatement(newstatement,cassignmentnode.create(
  1464. orgcode,
  1465. ctypeconvnode.create_internal(
  1466. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1467. { release the temp if we allocated one }
  1468. if assigned(tempcode) then
  1469. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1470. { free the errornode }
  1471. result.free;
  1472. { and return it }
  1473. result := newblock;
  1474. end;
  1475. function tinlinenode.handle_setlength: tnode;
  1476. var
  1477. def: tdef;
  1478. destppn,
  1479. paras: tnode;
  1480. newstatement: tstatementnode;
  1481. ppn: tcallparanode;
  1482. counter,
  1483. dims: longint;
  1484. isarray: boolean;
  1485. begin
  1486. { for easy exiting if something goes wrong }
  1487. result:=cerrornode.create;
  1488. resultdef:=voidtype;
  1489. paras:=left;
  1490. dims:=0;
  1491. if assigned(paras) then
  1492. begin
  1493. { check type of lengths }
  1494. ppn:=tcallparanode(paras);
  1495. while assigned(ppn.right) do
  1496. begin
  1497. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1498. inserttypeconv(ppn.left,sinttype);
  1499. inc(dims);
  1500. ppn:=tcallparanode(ppn.right);
  1501. end;
  1502. end
  1503. else
  1504. internalerror(2013112912);
  1505. if dims=0 then
  1506. begin
  1507. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1508. exit;
  1509. end;
  1510. { last param must be var }
  1511. destppn:=ppn.left;
  1512. valid_for_var(destppn,true);
  1513. set_varstate(destppn,vs_written,[]);
  1514. { first param must be a string or dynamic array ...}
  1515. isarray:=is_dynamic_array(destppn.resultdef);
  1516. if not((destppn.resultdef.typ=stringdef) or
  1517. isarray) then
  1518. begin
  1519. { possibly generic involved? }
  1520. if df_generic in current_procinfo.procdef.defoptions then
  1521. result:=internalstatements(newstatement)
  1522. else
  1523. CGMessage(type_e_mismatch);
  1524. exit;
  1525. end;
  1526. { only dynamic arrays accept more dimensions }
  1527. if (dims>1) then
  1528. begin
  1529. if (not isarray) then
  1530. CGMessage(type_e_mismatch)
  1531. else
  1532. begin
  1533. { check if the amount of dimensions is valid }
  1534. def:=tarraydef(destppn.resultdef).elementdef;
  1535. counter:=dims;
  1536. while counter > 1 do
  1537. begin
  1538. if not(is_dynamic_array(def)) then
  1539. begin
  1540. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1541. break;
  1542. end;
  1543. dec(counter);
  1544. def:=tarraydef(def).elementdef;
  1545. end;
  1546. end;
  1547. end;
  1548. result.free;
  1549. result:=nil;
  1550. end;
  1551. function tinlinenode.handle_copy: tnode;
  1552. var
  1553. paras : tnode;
  1554. ppn : tcallparanode;
  1555. paradef : tdef;
  1556. counter : integer;
  1557. begin
  1558. result:=nil;
  1559. { determine copy function to use based on the first argument,
  1560. also count the number of arguments in this loop }
  1561. counter:=1;
  1562. paras:=left;
  1563. ppn:=tcallparanode(paras);
  1564. while assigned(ppn.right) do
  1565. begin
  1566. inc(counter);
  1567. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1568. ppn:=tcallparanode(ppn.right);
  1569. end;
  1570. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1571. paradef:=ppn.left.resultdef;
  1572. if is_ansistring(paradef) then
  1573. // set resultdef to argument def
  1574. resultdef:=paradef
  1575. else if (is_chararray(paradef) and (paradef.size>255)) or
  1576. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1577. // set resultdef to ansistring type since result will be in ansistring codepage
  1578. resultdef:=getansistringdef
  1579. else
  1580. if is_widestring(paradef) then
  1581. resultdef:=cwidestringtype
  1582. else
  1583. if is_unicodestring(paradef) or
  1584. is_widechararray(paradef) or
  1585. is_pwidechar(paradef) then
  1586. resultdef:=cunicodestringtype
  1587. else
  1588. if is_char(paradef) then
  1589. resultdef:=cshortstringtype
  1590. else
  1591. if is_dynamic_array(paradef) then
  1592. begin
  1593. { Only allow 1 or 3 arguments }
  1594. if not(counter in [1..3]) then
  1595. begin
  1596. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  1597. exit;
  1598. end;
  1599. resultdef:=paradef;
  1600. end
  1601. else
  1602. begin
  1603. { generic fallback that will give an error if a wrong
  1604. type is passed }
  1605. if (counter=3) then
  1606. resultdef:=cshortstringtype
  1607. else
  1608. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  1609. end;
  1610. end;
  1611. {$maxfpuregisters 0}
  1612. function getpi : bestreal;
  1613. begin
  1614. {$ifdef x86}
  1615. { x86 has pi in hardware }
  1616. result:=pi;
  1617. {$else x86}
  1618. {$ifdef cpuextended}
  1619. result:=MathPiExtended.Value;
  1620. {$else cpuextended}
  1621. result:=MathPi.Value;
  1622. {$endif cpuextended}
  1623. {$endif x86}
  1624. end;
  1625. function tinlinenode.simplify(forinline : boolean): tnode;
  1626. function do_lowhigh(def:tdef) : tnode;
  1627. var
  1628. v : tconstexprint;
  1629. enum : tenumsym;
  1630. hp : tnode;
  1631. i : integer;
  1632. begin
  1633. case def.typ of
  1634. orddef:
  1635. begin
  1636. set_varstate(left,vs_read,[]);
  1637. if inlinenumber=in_low_x then
  1638. v:=torddef(def).low
  1639. else
  1640. v:=torddef(def).high;
  1641. hp:=cordconstnode.create(v,def,true);
  1642. typecheckpass(hp);
  1643. do_lowhigh:=hp;
  1644. end;
  1645. enumdef:
  1646. begin
  1647. set_varstate(left,vs_read,[]);
  1648. if inlinenumber=in_high_x then
  1649. v:=tenumdef(def).maxval
  1650. else
  1651. v:=tenumdef(def).minval;
  1652. enum:=nil;
  1653. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1654. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1655. begin
  1656. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1657. break;
  1658. end;
  1659. if not assigned(enum) then
  1660. internalerror(309993)
  1661. else
  1662. hp:=genenumnode(enum);
  1663. do_lowhigh:=hp;
  1664. end;
  1665. else
  1666. internalerror(87);
  1667. end;
  1668. end;
  1669. function getconstrealvalue : bestreal;
  1670. begin
  1671. case left.nodetype of
  1672. ordconstn:
  1673. getconstrealvalue:=tordconstnode(left).value;
  1674. realconstn:
  1675. getconstrealvalue:=trealconstnode(left).value_real;
  1676. else
  1677. internalerror(309992);
  1678. end;
  1679. end;
  1680. procedure setconstrealvalue(r : bestreal);
  1681. begin
  1682. result:=crealconstnode.create(r,pbestrealtype^);
  1683. end;
  1684. function handle_ln_const(r : bestreal) : tnode;
  1685. begin
  1686. if r<=0.0 then
  1687. if floating_point_range_check_error then
  1688. begin
  1689. result:=crealconstnode.create(0,pbestrealtype^);
  1690. CGMessage(type_e_wrong_math_argument)
  1691. end
  1692. else
  1693. begin
  1694. if r=0.0 then
  1695. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1696. else
  1697. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1698. end
  1699. else
  1700. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1701. end;
  1702. function handle_sqrt_const(r : bestreal) : tnode;
  1703. begin
  1704. if r<0.0 then
  1705. if floating_point_range_check_error then
  1706. begin
  1707. result:=crealconstnode.create(0,pbestrealtype^);
  1708. CGMessage(type_e_wrong_math_argument)
  1709. end
  1710. else
  1711. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1712. else
  1713. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1714. end;
  1715. function handle_const_sar : tnode;
  1716. var
  1717. vl,vl2 : TConstExprInt;
  1718. bits,shift: integer;
  1719. mask : qword;
  1720. def : tdef;
  1721. begin
  1722. result:=nil;
  1723. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1724. begin
  1725. if (left.nodetype=callparan) and
  1726. assigned(tcallparanode(left).right) then
  1727. begin
  1728. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1729. begin
  1730. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1731. vl:=tordconstnode(tcallparanode(left).left).value;
  1732. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1733. end
  1734. else
  1735. exit;
  1736. end
  1737. else
  1738. begin
  1739. def:=left.resultdef;
  1740. vl:=1;
  1741. vl2:=tordconstnode(left).value;
  1742. end;
  1743. bits:=def.size*8;
  1744. shift:=vl.svalue and (bits-1);
  1745. case bits of
  1746. 8:
  1747. mask:=$ff;
  1748. 16:
  1749. mask:=$ffff;
  1750. 32:
  1751. mask:=$ffffffff;
  1752. 64:
  1753. mask:=qword($ffffffffffffffff);
  1754. else
  1755. mask:=qword(1 shl bits)-1;
  1756. end;
  1757. {$push}
  1758. {$r-,q-}
  1759. if shift=0 then
  1760. result:=cordconstnode.create(vl2.svalue,def,false)
  1761. else if vl2.svalue<0 then
  1762. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1763. else
  1764. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1765. {$pop}
  1766. end
  1767. else
  1768. end;
  1769. function handle_const_rox : tnode;
  1770. var
  1771. vl,vl2 : TConstExprInt;
  1772. bits,shift: integer;
  1773. def : tdef;
  1774. begin
  1775. result:=nil;
  1776. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1777. begin
  1778. if (left.nodetype=callparan) and
  1779. assigned(tcallparanode(left).right) then
  1780. begin
  1781. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1782. begin
  1783. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1784. vl:=tordconstnode(tcallparanode(left).left).value;
  1785. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1786. end
  1787. else
  1788. exit;
  1789. end
  1790. else
  1791. begin
  1792. def:=left.resultdef;
  1793. vl:=1;
  1794. vl2:=tordconstnode(left).value;
  1795. end;
  1796. bits:=def.size*8;
  1797. shift:=vl.svalue and (bits-1);
  1798. {$push}
  1799. {$r-,q-}
  1800. if shift=0 then
  1801. result:=cordconstnode.create(vl2.svalue,def,false)
  1802. else
  1803. case inlinenumber of
  1804. in_ror_x,in_ror_x_y:
  1805. case def.size of
  1806. 1:
  1807. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1808. 2:
  1809. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1810. 4:
  1811. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1812. 8:
  1813. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1814. else
  1815. internalerror(2011061903);
  1816. end;
  1817. in_rol_x,in_rol_x_y:
  1818. case def.size of
  1819. 1:
  1820. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1821. 2:
  1822. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1823. 4:
  1824. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1825. 8:
  1826. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1827. else
  1828. internalerror(2011061902);
  1829. end;
  1830. else
  1831. internalerror(2011061901);
  1832. end;
  1833. end;
  1834. end;
  1835. var
  1836. hp : tnode;
  1837. vl,vl2 : TConstExprInt;
  1838. vr : bestreal;
  1839. begin { simplify }
  1840. result:=nil;
  1841. { handle intern constant functions in separate case }
  1842. if nf_inlineconst in flags then
  1843. begin
  1844. { no parameters? }
  1845. if not assigned(left) then
  1846. internalerror(200501231)
  1847. else
  1848. begin
  1849. vl:=0;
  1850. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1851. case left.nodetype of
  1852. realconstn :
  1853. begin
  1854. { Real functions are all handled with internproc below }
  1855. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1856. end;
  1857. ordconstn :
  1858. vl:=tordconstnode(left).value;
  1859. callparan :
  1860. begin
  1861. { both exists, else it was not generated }
  1862. vl:=tordconstnode(tcallparanode(left).left).value;
  1863. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1864. end;
  1865. else
  1866. CGMessage(parser_e_illegal_expression);
  1867. end;
  1868. case inlinenumber of
  1869. in_const_abs :
  1870. if vl.signed then
  1871. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1872. else
  1873. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1874. in_const_sqr:
  1875. if vl.signed then
  1876. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1877. else
  1878. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1879. in_const_odd :
  1880. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1881. in_const_swap_word :
  1882. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1883. in_const_swap_long :
  1884. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1885. in_const_swap_qword :
  1886. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1887. in_const_ptr:
  1888. begin
  1889. {Don't construct pointers from negative values.}
  1890. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1891. cgmessage(parser_e_range_check_error);
  1892. {$if defined(i8086)}
  1893. hp:=cpointerconstnode.create((vl2.uvalue shl 16)+vl.uvalue,voidfarpointertype);
  1894. {$elseif defined(i386)}
  1895. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidnearfspointertype);
  1896. {$else}
  1897. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidpointertype);
  1898. {$endif}
  1899. end
  1900. else
  1901. internalerror(88);
  1902. end;
  1903. end;
  1904. if hp=nil then
  1905. hp:=cerrornode.create;
  1906. result:=hp;
  1907. end
  1908. else
  1909. begin
  1910. case inlinenumber of
  1911. in_lo_long,
  1912. in_hi_long,
  1913. in_lo_qword,
  1914. in_hi_qword,
  1915. in_lo_word,
  1916. in_hi_word :
  1917. begin
  1918. if left.nodetype=ordconstn then
  1919. begin
  1920. case inlinenumber of
  1921. in_lo_word :
  1922. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1923. in_hi_word :
  1924. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1925. in_lo_long :
  1926. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1927. in_hi_long :
  1928. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1929. in_lo_qword :
  1930. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1931. in_hi_qword :
  1932. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1933. end;
  1934. end;
  1935. end;
  1936. in_ord_x:
  1937. begin
  1938. case left.resultdef.typ of
  1939. orddef :
  1940. begin
  1941. case torddef(left.resultdef).ordtype of
  1942. pasbool8,
  1943. uchar:
  1944. begin
  1945. { change to byte() }
  1946. result:=ctypeconvnode.create_internal(left,u8inttype);
  1947. left:=nil;
  1948. end;
  1949. pasbool16,
  1950. uwidechar :
  1951. begin
  1952. { change to word() }
  1953. result:=ctypeconvnode.create_internal(left,u16inttype);
  1954. left:=nil;
  1955. end;
  1956. pasbool32 :
  1957. begin
  1958. { change to dword() }
  1959. result:=ctypeconvnode.create_internal(left,u32inttype);
  1960. left:=nil;
  1961. end;
  1962. pasbool64 :
  1963. begin
  1964. { change to qword() }
  1965. result:=ctypeconvnode.create_internal(left,u64inttype);
  1966. left:=nil;
  1967. end;
  1968. bool8bit:
  1969. begin
  1970. { change to shortint() }
  1971. result:=ctypeconvnode.create_internal(left,s8inttype);
  1972. left:=nil;
  1973. end;
  1974. bool16bit :
  1975. begin
  1976. { change to smallint() }
  1977. result:=ctypeconvnode.create_internal(left,s16inttype);
  1978. left:=nil;
  1979. end;
  1980. bool32bit :
  1981. begin
  1982. { change to longint() }
  1983. result:=ctypeconvnode.create_internal(left,s32inttype);
  1984. left:=nil;
  1985. end;
  1986. bool64bit :
  1987. begin
  1988. { change to int64() }
  1989. result:=ctypeconvnode.create_internal(left,s64inttype);
  1990. left:=nil;
  1991. end;
  1992. uvoid :
  1993. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1994. else
  1995. begin
  1996. { all other orddef need no transformation }
  1997. result:=left;
  1998. left:=nil;
  1999. end;
  2000. end;
  2001. end;
  2002. enumdef :
  2003. begin
  2004. result:=ctypeconvnode.create_internal(left,s32inttype);
  2005. left:=nil;
  2006. end;
  2007. pointerdef :
  2008. begin
  2009. if m_mac in current_settings.modeswitches then
  2010. begin
  2011. result:=ctypeconvnode.create_internal(left,ptruinttype);
  2012. left:=nil;
  2013. end
  2014. end;
  2015. end;
  2016. (*
  2017. if (left.nodetype=ordconstn) then
  2018. begin
  2019. result:=cordconstnode.create(
  2020. tordconstnode(left).value,sinttype,true);
  2021. end
  2022. else if (m_mac in current_settings.modeswitches) and
  2023. (left.ndoetype=pointerconstn) then
  2024. result:=cordconstnode.create(
  2025. tpointerconstnode(left).value,ptruinttype,true);
  2026. *)
  2027. end;
  2028. in_chr_byte:
  2029. begin
  2030. { convert to explicit char() }
  2031. result:=ctypeconvnode.create_internal(left,cansichartype);
  2032. left:=nil;
  2033. end;
  2034. in_length_x:
  2035. begin
  2036. case left.resultdef.typ of
  2037. stringdef :
  2038. begin
  2039. if (left.nodetype=stringconstn) then
  2040. begin
  2041. result:=cordconstnode.create(
  2042. tstringconstnode(left).len,sinttype,true);
  2043. end;
  2044. end;
  2045. orddef :
  2046. begin
  2047. { length of char is always one }
  2048. if is_char(left.resultdef) or
  2049. is_widechar(left.resultdef) then
  2050. begin
  2051. result:=cordconstnode.create(1,sinttype,false);
  2052. end
  2053. end;
  2054. arraydef :
  2055. begin
  2056. if (left.nodetype=stringconstn) then
  2057. begin
  2058. result:=cordconstnode.create(
  2059. tstringconstnode(left).len,sinttype,true);
  2060. end
  2061. else if not is_open_array(left.resultdef) and
  2062. not is_array_of_const(left.resultdef) and
  2063. not is_dynamic_array(left.resultdef) then
  2064. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  2065. tarraydef(left.resultdef).lowrange+1,
  2066. sinttype,true);
  2067. end;
  2068. end;
  2069. end;
  2070. in_assigned_x:
  2071. begin
  2072. if is_constnode(tcallparanode(left).left) or
  2073. (tcallparanode(left).left.nodetype = pointerconstn) then
  2074. begin
  2075. { let an add node figure it out }
  2076. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  2077. tcallparanode(left).left := nil;
  2078. end;
  2079. end;
  2080. in_pred_x,
  2081. in_succ_x:
  2082. begin
  2083. if (left.nodetype=ordconstn) then
  2084. begin
  2085. if (inlinenumber=in_succ_x) then
  2086. vl:=tordconstnode(left).value+1
  2087. else
  2088. vl:=tordconstnode(left).value-1;
  2089. if is_integer(left.resultdef) then
  2090. { the type of the original integer constant is irrelevant,
  2091. it should be automatically adapted to the new value
  2092. (except when inlining) }
  2093. result:=create_simplified_ord_const(vl,resultdef,forinline)
  2094. else
  2095. { check the range for enums, chars, booleans }
  2096. result:=cordconstnode.create(vl,left.resultdef,true)
  2097. end
  2098. end;
  2099. in_low_x,
  2100. in_high_x:
  2101. begin
  2102. case left.resultdef.typ of
  2103. orddef,
  2104. enumdef:
  2105. begin
  2106. result:=do_lowhigh(left.resultdef);
  2107. end;
  2108. setdef:
  2109. begin
  2110. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  2111. end;
  2112. arraydef:
  2113. begin
  2114. if (inlinenumber=in_low_x) then
  2115. begin
  2116. result:=cordconstnode.create(int64(tarraydef(
  2117. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2118. end
  2119. else if not is_open_array(left.resultdef) and
  2120. not is_array_of_const(left.resultdef) and
  2121. not is_dynamic_array(left.resultdef) then
  2122. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2123. tarraydef(left.resultdef).rangedef,true);
  2124. end;
  2125. stringdef:
  2126. begin
  2127. if inlinenumber=in_low_x then
  2128. begin
  2129. if is_dynamicstring(left.resultdef) and
  2130. not(cs_zerobasedstrings in current_settings.localswitches) then
  2131. result:=cordconstnode.create(1,u8inttype,false)
  2132. else
  2133. result:=cordconstnode.create(0,u8inttype,false);
  2134. end
  2135. else if not is_dynamicstring(left.resultdef) then
  2136. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2137. end;
  2138. end;
  2139. end;
  2140. in_exp_real :
  2141. begin
  2142. if left.nodetype in [ordconstn,realconstn] then
  2143. begin
  2144. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2145. if (trealconstnode(result).value_real=MathInf.Value) and
  2146. floating_point_range_check_error then
  2147. begin
  2148. result:=crealconstnode.create(0,pbestrealtype^);
  2149. CGMessage(parser_e_range_check_error);
  2150. end;
  2151. end
  2152. end;
  2153. in_trunc_real :
  2154. begin
  2155. if left.nodetype in [ordconstn,realconstn] then
  2156. begin
  2157. vr:=getconstrealvalue;
  2158. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2159. begin
  2160. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2161. result:=cordconstnode.create(1,s64inttype,false)
  2162. end
  2163. else
  2164. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2165. end
  2166. end;
  2167. in_round_real :
  2168. begin
  2169. { can't evaluate while inlining, may depend on fpu setting }
  2170. if (not forinline) and
  2171. (left.nodetype in [ordconstn,realconstn]) then
  2172. begin
  2173. vr:=getconstrealvalue;
  2174. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2175. begin
  2176. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2177. result:=cordconstnode.create(1,s64inttype,false)
  2178. end
  2179. else
  2180. result:=cordconstnode.create(round(vr),s64inttype,true)
  2181. end
  2182. end;
  2183. in_frac_real :
  2184. begin
  2185. if left.nodetype in [ordconstn,realconstn] then
  2186. setconstrealvalue(frac(getconstrealvalue))
  2187. end;
  2188. in_int_real :
  2189. begin
  2190. if left.nodetype in [ordconstn,realconstn] then
  2191. setconstrealvalue(int(getconstrealvalue));
  2192. end;
  2193. in_pi_real :
  2194. begin
  2195. if block_type=bt_const then
  2196. setconstrealvalue(getpi)
  2197. end;
  2198. in_cos_real :
  2199. begin
  2200. if left.nodetype in [ordconstn,realconstn] then
  2201. setconstrealvalue(cos(getconstrealvalue))
  2202. end;
  2203. in_sin_real :
  2204. begin
  2205. if left.nodetype in [ordconstn,realconstn] then
  2206. setconstrealvalue(sin(getconstrealvalue))
  2207. end;
  2208. in_arctan_real :
  2209. begin
  2210. if left.nodetype in [ordconstn,realconstn] then
  2211. setconstrealvalue(arctan(getconstrealvalue))
  2212. end;
  2213. in_abs_real :
  2214. begin
  2215. if left.nodetype in [ordconstn,realconstn] then
  2216. setconstrealvalue(abs(getconstrealvalue))
  2217. end;
  2218. in_abs_long:
  2219. begin
  2220. if left.nodetype=ordconstn then
  2221. begin
  2222. if tordconstnode(left).value<0 then
  2223. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  2224. else
  2225. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  2226. end
  2227. end;
  2228. in_sqr_real :
  2229. begin
  2230. if left.nodetype in [ordconstn,realconstn] then
  2231. setconstrealvalue(sqr(getconstrealvalue))
  2232. end;
  2233. in_sqrt_real :
  2234. begin
  2235. if left.nodetype in [ordconstn,realconstn] then
  2236. begin
  2237. vr:=getconstrealvalue;
  2238. if vr<0.0 then
  2239. result:=handle_sqrt_const(vr)
  2240. else
  2241. setconstrealvalue(sqrt(vr));
  2242. end
  2243. end;
  2244. in_ln_real :
  2245. begin
  2246. if left.nodetype in [ordconstn,realconstn] then
  2247. begin
  2248. vr:=getconstrealvalue;
  2249. if vr<=0.0 then
  2250. result:=handle_ln_const(vr)
  2251. else
  2252. setconstrealvalue(ln(vr));
  2253. end
  2254. end;
  2255. in_assert_x_y :
  2256. begin
  2257. if not(cs_do_assertion in current_settings.localswitches) then
  2258. { we need a valid node, so insert a nothingn }
  2259. result:=cnothingnode.create;
  2260. end;
  2261. in_sar_x,
  2262. in_sar_x_y :
  2263. begin
  2264. result:=handle_const_sar;
  2265. end;
  2266. in_rol_x,
  2267. in_rol_x_y,
  2268. in_ror_x,
  2269. in_ror_x_y :
  2270. result:=handle_const_rox;
  2271. end;
  2272. end;
  2273. end;
  2274. function tinlinenode.pass_typecheck:tnode;
  2275. procedure setfloatresultdef;
  2276. begin
  2277. if (left.resultdef.typ=floatdef) and
  2278. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2279. resultdef:=left.resultdef
  2280. else
  2281. begin
  2282. if (left.nodetype <> ordconstn) then
  2283. inserttypeconv(left,pbestrealtype^);
  2284. resultdef:=pbestrealtype^;
  2285. end;
  2286. end;
  2287. procedure handle_pack_unpack;
  2288. var
  2289. source, target, index: tcallparanode;
  2290. unpackedarraydef, packedarraydef: tarraydef;
  2291. tempindex: TConstExprInt;
  2292. begin
  2293. resultdef:=voidtype;
  2294. unpackedarraydef := nil;
  2295. packedarraydef := nil;
  2296. source := tcallparanode(left);
  2297. if (inlinenumber = in_unpack_x_y_z) then
  2298. begin
  2299. target := tcallparanode(source.right);
  2300. index := tcallparanode(target.right);
  2301. { source must be a packed array }
  2302. if not is_packed_array(source.left.resultdef) then
  2303. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2304. else
  2305. packedarraydef := tarraydef(source.left.resultdef);
  2306. { target can be any kind of array, as long as it's not packed }
  2307. if (target.left.resultdef.typ <> arraydef) or
  2308. is_packed_array(target.left.resultdef) then
  2309. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2310. else
  2311. unpackedarraydef := tarraydef(target.left.resultdef);
  2312. end
  2313. else
  2314. begin
  2315. index := tcallparanode(source.right);
  2316. target := tcallparanode(index.right);
  2317. { source can be any kind of array, as long as it's not packed }
  2318. if (source.left.resultdef.typ <> arraydef) or
  2319. is_packed_array(source.left.resultdef) then
  2320. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2321. else
  2322. unpackedarraydef := tarraydef(source.left.resultdef);
  2323. { target must be a packed array }
  2324. if not is_packed_array(target.left.resultdef) then
  2325. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2326. else
  2327. packedarraydef := tarraydef(target.left.resultdef);
  2328. end;
  2329. if assigned(unpackedarraydef) then
  2330. begin
  2331. { index must be compatible with the unpacked array's indextype }
  2332. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2333. { range check at compile time if possible }
  2334. if assigned(packedarraydef) and
  2335. (index.left.nodetype = ordconstn) and
  2336. not is_special_array(unpackedarraydef) then
  2337. begin
  2338. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2339. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2340. testrange(unpackedarraydef,tempindex,false,false);
  2341. end;
  2342. end;
  2343. { source array is read and must be valid }
  2344. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2345. { target array is written }
  2346. valid_for_assignment(target.left,true);
  2347. set_varstate(target.left,vs_written,[]);
  2348. { index in the unpacked array is read and must be valid }
  2349. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2350. { if the size of the arrays is 0 (array of empty records), }
  2351. { do nothing }
  2352. if (source.resultdef.size = 0) then
  2353. result:=cnothingnode.create;
  2354. end;
  2355. function handle_objc_encode: tnode;
  2356. var
  2357. encodedtype: ansistring;
  2358. errordef: tdef;
  2359. begin
  2360. encodedtype:='';
  2361. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2362. Message1(type_e_objc_type_unsupported,errordef.typename);
  2363. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype),nil);
  2364. end;
  2365. var
  2366. hightree,
  2367. hp : tnode;
  2368. temp_pnode: pnode;
  2369. begin
  2370. result:=nil;
  2371. { when handling writeln "left" contains no valid address }
  2372. if assigned(left) then
  2373. begin
  2374. if left.nodetype=callparan then
  2375. tcallparanode(left).get_paratype
  2376. else
  2377. typecheckpass(left);
  2378. end;
  2379. if not(nf_inlineconst in flags) then
  2380. begin
  2381. case inlinenumber of
  2382. in_lo_long,
  2383. in_hi_long,
  2384. in_lo_qword,
  2385. in_hi_qword,
  2386. in_lo_word,
  2387. in_hi_word :
  2388. begin
  2389. { give warning for incompatibility with tp and delphi }
  2390. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2391. ((m_tp7 in current_settings.modeswitches) or
  2392. (m_delphi in current_settings.modeswitches)) then
  2393. CGMessage(type_w_maybe_wrong_hi_lo);
  2394. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2395. if not is_integer(left.resultdef) then
  2396. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2397. case inlinenumber of
  2398. in_lo_word,
  2399. in_hi_word :
  2400. resultdef:=u8inttype;
  2401. in_lo_long,
  2402. in_hi_long :
  2403. resultdef:=u16inttype;
  2404. in_lo_qword,
  2405. in_hi_qword :
  2406. resultdef:=u32inttype;
  2407. end;
  2408. end;
  2409. in_sizeof_x:
  2410. begin
  2411. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2412. set_varstate(left,vs_read,[]);
  2413. if (left.resultdef.typ<>undefineddef) and
  2414. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2415. begin
  2416. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2417. if assigned(hightree) then
  2418. begin
  2419. hp:=caddnode.create(addn,hightree,
  2420. cordconstnode.create(1,sinttype,false));
  2421. if (left.resultdef.typ=arraydef) then
  2422. if not is_packed_array(tarraydef(left.resultdef)) then
  2423. begin
  2424. if (tarraydef(left.resultdef).elesize<>1) then
  2425. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2426. left.resultdef).elesize,sinttype,true));
  2427. end
  2428. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2429. begin
  2430. { no packed open array support yet }
  2431. if (hp.nodetype <> ordconstn) then
  2432. internalerror(2006081511);
  2433. hp.free;
  2434. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2435. {
  2436. hp:=
  2437. ctypeconvnode.create_explicit(sinttype,
  2438. cmoddivnode.create(divn,
  2439. caddnode.create(addn,
  2440. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2441. left.resultdef).elepackedbitsize,s64inttype,true)),
  2442. cordconstnode.create(a,s64inttype,true)),
  2443. cordconstnode.create(8,s64inttype,true)),
  2444. sinttype);
  2445. }
  2446. end;
  2447. result:=hp;
  2448. end;
  2449. end
  2450. else
  2451. resultdef:=sinttype;
  2452. end;
  2453. in_typeof_x:
  2454. begin
  2455. if target_info.system in systems_managed_vm then
  2456. message(parser_e_feature_unsupported_for_vm);
  2457. typecheckpass(left);
  2458. set_varstate(left,vs_read,[]);
  2459. if (left.resultdef.typ=objectdef) and
  2460. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2461. message(type_e_typeof_requires_vmt);
  2462. resultdef:=voidpointertype;
  2463. end;
  2464. in_ord_x:
  2465. begin
  2466. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2467. case left.resultdef.typ of
  2468. orddef,
  2469. enumdef :
  2470. ;
  2471. pointerdef :
  2472. begin
  2473. if not(m_mac in current_settings.modeswitches) then
  2474. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2475. end
  2476. else
  2477. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2478. end;
  2479. end;
  2480. in_chr_byte:
  2481. begin
  2482. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2483. end;
  2484. in_length_x:
  2485. begin
  2486. if ((left.resultdef.typ=arraydef) and
  2487. (not is_special_array(left.resultdef) or
  2488. is_open_array(left.resultdef))) or
  2489. (left.resultdef.typ=orddef) then
  2490. set_varstate(left,vs_read,[])
  2491. else
  2492. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2493. case left.resultdef.typ of
  2494. variantdef:
  2495. begin
  2496. inserttypeconv(left,getansistringdef);
  2497. end;
  2498. stringdef :
  2499. begin
  2500. { we don't need string convertions here, }
  2501. { except if from widestring to ansistring }
  2502. { and vice versa (that can change the }
  2503. { length) }
  2504. if (left.nodetype=typeconvn) and
  2505. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2506. not(is_wide_or_unicode_string(left.resultdef) xor
  2507. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2508. begin
  2509. hp:=ttypeconvnode(left).left;
  2510. ttypeconvnode(left).left:=nil;
  2511. left.free;
  2512. left:=hp;
  2513. end;
  2514. end;
  2515. orddef :
  2516. begin
  2517. { will be handled in simplify }
  2518. if not is_char(left.resultdef) and
  2519. not is_widechar(left.resultdef) then
  2520. CGMessage(type_e_mismatch);
  2521. end;
  2522. pointerdef :
  2523. begin
  2524. if is_pchar(left.resultdef) then
  2525. begin
  2526. hp := ccallparanode.create(left,nil);
  2527. result := ccallnode.createintern('fpc_pchar_length',hp);
  2528. { make sure the left node doesn't get disposed, since it's }
  2529. { reused in the new node (JM) }
  2530. left:=nil;
  2531. exit;
  2532. end
  2533. else if is_pwidechar(left.resultdef) then
  2534. begin
  2535. hp := ccallparanode.create(left,nil);
  2536. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2537. { make sure the left node doesn't get disposed, since it's }
  2538. { reused in the new node (JM) }
  2539. left:=nil;
  2540. exit;
  2541. end
  2542. else
  2543. CGMessage(type_e_mismatch);
  2544. end;
  2545. arraydef :
  2546. begin
  2547. if is_open_array(left.resultdef) or
  2548. is_array_of_const(left.resultdef) then
  2549. begin
  2550. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2551. if assigned(hightree) then
  2552. result:=caddnode.create(addn,hightree,
  2553. cordconstnode.create(1,sinttype,false));
  2554. exit;
  2555. end
  2556. { Length() for dynamic arrays is inlined }
  2557. else
  2558. begin
  2559. { will be handled in simplify }
  2560. end;
  2561. end
  2562. else
  2563. CGMessage(type_e_mismatch);
  2564. end;
  2565. { shortstring return an 8 bit value as the length
  2566. is the first byte of the string }
  2567. if is_shortstring(left.resultdef) then
  2568. resultdef:=u8inttype
  2569. else
  2570. resultdef:=sinttype;
  2571. end;
  2572. in_typeinfo_x:
  2573. begin
  2574. if target_info.system in systems_managed_vm then
  2575. message(parser_e_feature_unsupported_for_vm);
  2576. if (left.resultdef.typ=enumdef) and
  2577. (tenumdef(left.resultdef).has_jumps) then
  2578. CGMessage(type_e_no_type_info);
  2579. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2580. resultdef:=voidpointertype;
  2581. end;
  2582. in_assigned_x:
  2583. begin
  2584. { the parser has already made sure the expression is valid }
  2585. { in case of a complex procvar, only check the "code" pointer }
  2586. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2587. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2588. begin
  2589. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2590. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2591. tcallparanode(left).get_paratype;
  2592. end;
  2593. { Postpone conversion into addnode until firstpass, so targets
  2594. may override first_assigned and insert specific code. }
  2595. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2596. resultdef:=pasbool8type;
  2597. end;
  2598. in_ofs_x :
  2599. internalerror(2000101001);
  2600. in_seg_x :
  2601. begin
  2602. result := typecheck_seg;
  2603. end;
  2604. in_pred_x,
  2605. in_succ_x:
  2606. begin
  2607. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2608. resultdef:=left.resultdef;
  2609. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  2610. begin
  2611. if (resultdef.typ=enumdef) and
  2612. (tenumdef(resultdef).has_jumps) and
  2613. not(m_delphi in current_settings.modeswitches) then
  2614. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2615. end
  2616. else
  2617. CGMessage(type_e_ordinal_expr_expected)
  2618. end;
  2619. in_copy_x:
  2620. result:=handle_copy;
  2621. in_initialize_x,
  2622. in_finalize_x:
  2623. begin
  2624. { inlined from pinline }
  2625. internalerror(200204231);
  2626. end;
  2627. in_setlength_x:
  2628. begin
  2629. result:=handle_setlength;
  2630. end;
  2631. in_inc_x,
  2632. in_dec_x:
  2633. begin
  2634. resultdef:=voidtype;
  2635. if assigned(left) then
  2636. begin
  2637. { first param must be var }
  2638. valid_for_var(tcallparanode(left).left,true);
  2639. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2640. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2641. is_ordinal(left.resultdef) or
  2642. is_currency(left.resultdef) then
  2643. begin
  2644. { value of left gets changed -> must be unique }
  2645. set_unique(tcallparanode(left).left);
  2646. { two paras ? }
  2647. if assigned(tcallparanode(left).right) then
  2648. begin
  2649. if is_integer(tcallparanode(left).right.resultdef) then
  2650. begin
  2651. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2652. { when range/overflow checking is on, we
  2653. convert this to a regular add, and for proper
  2654. checking we need the original type }
  2655. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2656. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2657. begin
  2658. { don't convert values added to pointers into the pointer types themselves,
  2659. because that will turn signed values into unsigned ones, which then
  2660. goes wrong when they have to be multiplied with the size of the elements
  2661. to which the pointer points in ncginl (mantis #17342) }
  2662. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2663. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2664. else
  2665. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2666. end
  2667. else if is_integer(tcallparanode(left).left.resultdef) then
  2668. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2669. else
  2670. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2671. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2672. { should be handled in the parser (JM) }
  2673. internalerror(2006020901);
  2674. end
  2675. else
  2676. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2677. end;
  2678. end
  2679. { generic type parameter? }
  2680. else if is_typeparam(left.resultdef) then
  2681. begin
  2682. result:=cnothingnode.create;
  2683. exit;
  2684. end
  2685. else
  2686. begin
  2687. hp:=self;
  2688. if isunaryoverloaded(hp) then
  2689. begin
  2690. { inc(rec) and dec(rec) assigns result value to argument }
  2691. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2692. exit;
  2693. end
  2694. else
  2695. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2696. end;
  2697. end
  2698. else
  2699. CGMessagePos(fileinfo,type_e_mismatch);
  2700. end;
  2701. in_read_x,
  2702. in_readln_x,
  2703. in_readstr_x,
  2704. in_write_x,
  2705. in_writeln_x,
  2706. in_writestr_x :
  2707. begin
  2708. result := handle_read_write;
  2709. end;
  2710. in_settextbuf_file_x :
  2711. begin
  2712. if target_info.system in systems_managed_vm then
  2713. message(parser_e_feature_unsupported_for_vm);
  2714. resultdef:=voidtype;
  2715. { now we know the type of buffer }
  2716. hp:=ccallparanode.create(cordconstnode.create(
  2717. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2718. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2719. left:=nil;
  2720. end;
  2721. { the firstpass of the arg has been done in firstcalln ? }
  2722. in_reset_typedfile,
  2723. in_rewrite_typedfile :
  2724. begin
  2725. result := handle_reset_rewrite_typed;
  2726. end;
  2727. in_str_x_string :
  2728. begin
  2729. result:=handle_str;
  2730. end;
  2731. in_val_x :
  2732. begin
  2733. result:=handle_val;
  2734. end;
  2735. in_include_x_y,
  2736. in_exclude_x_y:
  2737. begin
  2738. resultdef:=voidtype;
  2739. { the parser already checks whether we have two (and exactly two) }
  2740. { parameters (JM) }
  2741. { first param must be var }
  2742. valid_for_var(tcallparanode(left).left,true);
  2743. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2744. { check type }
  2745. if (left.resultdef.typ=setdef) then
  2746. begin
  2747. { insert a type conversion }
  2748. { to the type of the set elements }
  2749. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2750. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2751. tsetdef(left.resultdef).elementdef);
  2752. end
  2753. else
  2754. CGMessage(type_e_mismatch);
  2755. end;
  2756. in_pack_x_y_z,
  2757. in_unpack_x_y_z :
  2758. begin
  2759. handle_pack_unpack;
  2760. end;
  2761. in_slice_x:
  2762. begin
  2763. if target_info.system in systems_managed_vm then
  2764. message(parser_e_feature_unsupported_for_vm);
  2765. result:=nil;
  2766. resultdef:=tcallparanode(left).left.resultdef;
  2767. if (resultdef.typ <> arraydef) then
  2768. CGMessagePos(left.fileinfo,type_e_mismatch)
  2769. else if is_packed_array(resultdef) then
  2770. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2771. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2772. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2773. type_e_integer_expr_expected,
  2774. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2775. end;
  2776. in_new_x:
  2777. resultdef:=left.resultdef;
  2778. in_low_x,
  2779. in_high_x:
  2780. begin
  2781. case left.resultdef.typ of
  2782. orddef,
  2783. enumdef,
  2784. setdef:
  2785. ;
  2786. arraydef:
  2787. begin
  2788. if (inlinenumber=in_low_x) then
  2789. set_varstate(left,vs_read,[])
  2790. else
  2791. begin
  2792. if is_open_array(left.resultdef) or
  2793. is_array_of_const(left.resultdef) then
  2794. begin
  2795. set_varstate(left,vs_read,[]);
  2796. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2797. end
  2798. else
  2799. if is_dynamic_array(left.resultdef) then
  2800. begin
  2801. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2802. { can't use inserttypeconv because we need }
  2803. { an explicit type conversion (JM) }
  2804. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2805. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2806. { make sure the left node doesn't get disposed, since it's }
  2807. { reused in the new node (JM) }
  2808. left:=nil;
  2809. end
  2810. else
  2811. begin
  2812. set_varstate(left,vs_read,[]);
  2813. end;
  2814. end;
  2815. end;
  2816. stringdef:
  2817. begin
  2818. if inlinenumber=in_low_x then
  2819. begin
  2820. set_varstate(left,vs_read,[]);
  2821. end
  2822. else
  2823. begin
  2824. if is_open_string(left.resultdef) then
  2825. begin
  2826. set_varstate(left,vs_read,[]);
  2827. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2828. end
  2829. else if is_dynamicstring(left.resultdef) then
  2830. begin
  2831. result:=cinlinenode.create(in_length_x,false,left);
  2832. if cs_zerobasedstrings in current_settings.localswitches then
  2833. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  2834. { make sure the left node doesn't get disposed, since it's }
  2835. { reused in the new node (JM) }
  2836. left:=nil;
  2837. end
  2838. end;
  2839. end;
  2840. else
  2841. CGMessage(type_e_mismatch);
  2842. end;
  2843. end;
  2844. in_exp_real,
  2845. in_frac_real,
  2846. in_int_real,
  2847. in_cos_real,
  2848. in_sin_real,
  2849. in_arctan_real,
  2850. in_abs_real,
  2851. in_ln_real :
  2852. begin
  2853. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2854. { converting an int64 to double on platforms without }
  2855. { extended can cause precision loss }
  2856. if not(left.nodetype in [ordconstn,realconstn]) then
  2857. inserttypeconv(left,pbestrealtype^);
  2858. resultdef:=pbestrealtype^;
  2859. end;
  2860. in_trunc_real,
  2861. in_round_real :
  2862. begin
  2863. { on i8086, the int64 result is returned in a var param, because
  2864. it's too big to fit in a register or a pair of registers. In
  2865. that case we have 2 parameters and left.nodetype is a callparan. }
  2866. if left.nodetype = callparan then
  2867. temp_pnode := @tcallparanode(left).left
  2868. else
  2869. temp_pnode := @left;
  2870. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  2871. { for direct float rounding, no best real type cast should be necessary }
  2872. if not((temp_pnode^.resultdef.typ=floatdef) and
  2873. (tfloatdef(temp_pnode^.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2874. { converting an int64 to double on platforms without }
  2875. { extended can cause precision loss }
  2876. not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  2877. inserttypeconv(temp_pnode^,pbestrealtype^);
  2878. resultdef:=s64inttype;
  2879. end;
  2880. in_pi_real :
  2881. begin
  2882. resultdef:=pbestrealtype^;
  2883. end;
  2884. in_abs_long:
  2885. begin
  2886. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2887. inserttypeconv(left,s32inttype);
  2888. resultdef:=s32inttype;
  2889. end;
  2890. in_sqr_real,
  2891. in_sqrt_real :
  2892. begin
  2893. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2894. setfloatresultdef;
  2895. end;
  2896. {$ifdef SUPPORT_MMX}
  2897. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2898. begin
  2899. end;
  2900. {$endif SUPPORT_MMX}
  2901. in_aligned_x,
  2902. in_unaligned_x:
  2903. begin
  2904. resultdef:=left.resultdef;
  2905. end;
  2906. in_assert_x_y :
  2907. begin
  2908. resultdef:=voidtype;
  2909. if assigned(left) then
  2910. begin
  2911. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2912. { check type }
  2913. if is_boolean(left.resultdef) then
  2914. begin
  2915. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2916. { must always be a string }
  2917. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2918. end
  2919. else
  2920. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2921. end
  2922. else
  2923. CGMessage(type_e_mismatch);
  2924. if (cs_do_assertion in current_settings.localswitches) then
  2925. include(current_procinfo.flags,pi_do_call);
  2926. end;
  2927. in_prefetch_var:
  2928. resultdef:=voidtype;
  2929. in_get_frame,
  2930. in_get_caller_frame,
  2931. in_get_caller_addr:
  2932. begin
  2933. resultdef:=voidpointertype;
  2934. end;
  2935. in_rol_x,
  2936. in_ror_x,
  2937. in_sar_x:
  2938. begin
  2939. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2940. resultdef:=left.resultdef;
  2941. end;
  2942. in_rol_x_y,
  2943. in_ror_x_y,
  2944. in_sar_x_y:
  2945. begin
  2946. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2947. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2948. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2949. end;
  2950. in_bsf_x,
  2951. in_bsr_x:
  2952. begin
  2953. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2954. if not is_integer(left.resultdef) then
  2955. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2956. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2957. resultdef:=u64inttype
  2958. else
  2959. resultdef:=u32inttype
  2960. end;
  2961. in_popcnt_x:
  2962. begin
  2963. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2964. if not is_integer(left.resultdef) then
  2965. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2966. resultdef:=left.resultdef;
  2967. end;
  2968. in_objc_selector_x:
  2969. begin
  2970. result:=cobjcselectornode.create(left);
  2971. { reused }
  2972. left:=nil;
  2973. end;
  2974. in_objc_protocol_x:
  2975. begin
  2976. result:=cobjcprotocolnode.create(left);
  2977. { reused }
  2978. left:=nil;
  2979. end;
  2980. in_objc_encode_x:
  2981. begin
  2982. result:=handle_objc_encode;
  2983. end;
  2984. in_default_x:
  2985. begin
  2986. result:=handle_default;
  2987. end;
  2988. in_box_x:
  2989. begin
  2990. result:=handle_box;
  2991. end;
  2992. in_unbox_x_y:
  2993. begin
  2994. result:=handle_unbox;
  2995. end;
  2996. else
  2997. internalerror(8);
  2998. end;
  2999. end;
  3000. if not assigned(result) and not
  3001. codegenerror then
  3002. result:=simplify(false);
  3003. end;
  3004. function tinlinenode.pass_1 : tnode;
  3005. var
  3006. hp,hpp,resultnode : tnode;
  3007. shiftconst: longint;
  3008. tempnode: ttempcreatenode;
  3009. newstatement: tstatementnode;
  3010. newblock: tblocknode;
  3011. begin
  3012. result:=nil;
  3013. { if we handle writeln; left contains no valid address }
  3014. if assigned(left) then
  3015. begin
  3016. if left.nodetype=callparan then
  3017. tcallparanode(left).firstcallparan
  3018. else
  3019. firstpass(left);
  3020. end;
  3021. { intern const should already be handled }
  3022. if nf_inlineconst in flags then
  3023. internalerror(200104044);
  3024. case inlinenumber of
  3025. in_lo_qword,
  3026. in_hi_qword,
  3027. in_lo_long,
  3028. in_hi_long,
  3029. in_lo_word,
  3030. in_hi_word:
  3031. begin
  3032. shiftconst := 0;
  3033. case inlinenumber of
  3034. in_hi_qword:
  3035. shiftconst := 32;
  3036. in_hi_long:
  3037. shiftconst := 16;
  3038. in_hi_word:
  3039. shiftconst := 8;
  3040. end;
  3041. if shiftconst <> 0 then
  3042. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  3043. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  3044. else
  3045. result := ctypeconvnode.create_internal(left,resultdef);
  3046. left := nil;
  3047. firstpass(result);
  3048. end;
  3049. in_sizeof_x,
  3050. in_typeof_x:
  3051. begin
  3052. expectloc:=LOC_REGISTER;
  3053. if (left.nodetype=typen) and
  3054. (cs_create_pic in current_settings.moduleswitches) and
  3055. (tf_pic_uses_got in target_info.flags) then
  3056. include(current_procinfo.flags,pi_needs_got);
  3057. end;
  3058. in_length_x:
  3059. begin
  3060. result:=first_length;
  3061. end;
  3062. in_typeinfo_x:
  3063. begin
  3064. result:=caddrnode.create_internal(
  3065. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  3066. );
  3067. end;
  3068. in_assigned_x:
  3069. begin
  3070. result:=first_assigned;
  3071. end;
  3072. in_pred_x,
  3073. in_succ_x:
  3074. begin
  3075. expectloc:=LOC_REGISTER;
  3076. { in case of range/overflow checking, use a regular addnode
  3077. because it's too complex to handle correctly otherwise }
  3078. {$ifndef jvm}
  3079. { enums are class instances in the JVM -> always need conversion }
  3080. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  3081. {$endif}
  3082. begin
  3083. { create constant 1 }
  3084. hp:=cordconstnode.create(1,left.resultdef,false);
  3085. typecheckpass(hp);
  3086. if not is_integer(hp.resultdef) then
  3087. inserttypeconv_internal(hp,sinttype);
  3088. { avoid type errors from the addn/subn }
  3089. if not is_integer(left.resultdef) then
  3090. inserttypeconv_internal(left,sinttype);
  3091. { addition/substraction depending on succ/pred }
  3092. if inlinenumber=in_succ_x then
  3093. hp:=caddnode.create(addn,left,hp)
  3094. else
  3095. hp:=caddnode.create(subn,left,hp);
  3096. { assign result of addition }
  3097. if not(is_integer(resultdef)) then
  3098. inserttypeconv(hp,torddef.create(
  3099. {$ifdef cpu64bitaddr}
  3100. s64bit,
  3101. {$else cpu64bitaddr}
  3102. s32bit,
  3103. {$endif cpu64bitaddr}
  3104. get_min_value(resultdef),
  3105. get_max_value(resultdef)))
  3106. else
  3107. inserttypeconv(hp,resultdef);
  3108. { avoid any possible errors/warnings }
  3109. inserttypeconv_internal(hp,resultdef);
  3110. { firstpass it }
  3111. firstpass(hp);
  3112. { left is reused }
  3113. left:=nil;
  3114. { return new node }
  3115. result:=hp;
  3116. end;
  3117. end;
  3118. in_setlength_x:
  3119. result:=first_setlength;
  3120. in_copy_x:
  3121. result:=first_copy;
  3122. in_initialize_x,
  3123. in_finalize_x:
  3124. begin
  3125. expectloc:=LOC_VOID;
  3126. end;
  3127. in_inc_x,
  3128. in_dec_x:
  3129. begin
  3130. expectloc:=LOC_VOID;
  3131. { range/overflow checking doesn't work properly }
  3132. { with the inc/dec code that's generated (JM) }
  3133. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3134. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3135. trigger an overflow. For uint32 it works because then the operation is done
  3136. in 64bit. Range checking is not applicable to pointers either }
  3137. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3138. {$ifdef jvm}
  3139. { enums are class instances on the JVM -> special treatment }
  3140. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3141. {$endif}
  3142. then
  3143. { convert to simple add (JM) }
  3144. begin
  3145. newblock := internalstatements(newstatement);
  3146. { extra parameter? }
  3147. if assigned(tcallparanode(left).right) then
  3148. begin
  3149. { Yes, use for add node }
  3150. hpp := tcallparanode(tcallparanode(left).right).left;
  3151. tcallparanode(tcallparanode(left).right).left := nil;
  3152. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3153. CGMessage(parser_e_illegal_expression);
  3154. end
  3155. else
  3156. begin
  3157. { no, create constant 1 }
  3158. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3159. end;
  3160. typecheckpass(hpp);
  3161. { make sure we don't call functions part of the left node twice (and generally }
  3162. { optimize the code generation) }
  3163. if node_complexity(tcallparanode(left).left) > 1 then
  3164. begin
  3165. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3166. addstatement(newstatement,tempnode);
  3167. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3168. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3169. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3170. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3171. end
  3172. else
  3173. begin
  3174. hp := tcallparanode(left).left.getcopy;
  3175. tempnode := nil;
  3176. end;
  3177. resultnode := hp.getcopy;
  3178. { avoid type errors from the addn/subn }
  3179. if not is_integer(resultnode.resultdef) then
  3180. begin
  3181. inserttypeconv_internal(hp,sinttype);
  3182. inserttypeconv_internal(hpp,sinttype);
  3183. end;
  3184. { addition/substraction depending on inc/dec }
  3185. if inlinenumber = in_inc_x then
  3186. hpp := caddnode.create(addn,hp,hpp)
  3187. else
  3188. hpp := caddnode.create(subn,hp,hpp);
  3189. { assign result of addition }
  3190. if not(is_integer(resultnode.resultdef)) then
  3191. inserttypeconv(hpp,torddef.create(
  3192. {$ifdef cpu64bitaddr}
  3193. s64bit,
  3194. {$else cpu64bitaddr}
  3195. s32bit,
  3196. {$endif cpu64bitaddr}
  3197. get_min_value(resultnode.resultdef),
  3198. get_max_value(resultnode.resultdef)))
  3199. else
  3200. inserttypeconv(hpp,resultnode.resultdef);
  3201. { avoid any possible warnings }
  3202. inserttypeconv_internal(hpp,resultnode.resultdef);
  3203. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3204. { deallocate the temp }
  3205. if assigned(tempnode) then
  3206. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3207. { firstpass it }
  3208. firstpass(tnode(newblock));
  3209. { return new node }
  3210. result := newblock;
  3211. end;
  3212. end;
  3213. in_include_x_y,
  3214. in_exclude_x_y:
  3215. begin
  3216. result:=first_IncludeExclude;
  3217. end;
  3218. in_pack_x_y_z,
  3219. in_unpack_x_y_z:
  3220. begin
  3221. result:=first_pack_unpack;
  3222. end;
  3223. in_exp_real:
  3224. begin
  3225. result:= first_exp_real;
  3226. end;
  3227. in_round_real:
  3228. begin
  3229. result:= first_round_real;
  3230. end;
  3231. in_trunc_real:
  3232. begin
  3233. result:= first_trunc_real;
  3234. end;
  3235. in_int_real:
  3236. begin
  3237. result:= first_int_real;
  3238. end;
  3239. in_frac_real:
  3240. begin
  3241. result:= first_frac_real;
  3242. end;
  3243. in_cos_real:
  3244. begin
  3245. result:= first_cos_real;
  3246. end;
  3247. in_sin_real:
  3248. begin
  3249. result := first_sin_real;
  3250. end;
  3251. in_arctan_real:
  3252. begin
  3253. result := first_arctan_real;
  3254. end;
  3255. in_pi_real :
  3256. begin
  3257. result := first_pi;
  3258. end;
  3259. in_abs_real:
  3260. begin
  3261. result := first_abs_real;
  3262. end;
  3263. in_abs_long:
  3264. begin
  3265. result := first_abs_long;
  3266. end;
  3267. in_sqr_real:
  3268. begin
  3269. result := first_sqr_real;
  3270. end;
  3271. in_sqrt_real:
  3272. begin
  3273. result := first_sqrt_real;
  3274. end;
  3275. in_ln_real:
  3276. begin
  3277. result := first_ln_real;
  3278. end;
  3279. {$ifdef SUPPORT_MMX}
  3280. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3281. begin
  3282. end;
  3283. {$endif SUPPORT_MMX}
  3284. in_assert_x_y :
  3285. begin
  3286. result:=first_assert;
  3287. end;
  3288. in_low_x,
  3289. in_high_x:
  3290. internalerror(200104047);
  3291. in_slice_x:
  3292. internalerror(2005101501);
  3293. in_ord_x,
  3294. in_chr_byte:
  3295. begin
  3296. { should not happend as it's converted to typeconv }
  3297. internalerror(200104045);
  3298. end;
  3299. in_ofs_x :
  3300. internalerror(2000101001);
  3301. in_seg_x :
  3302. begin
  3303. result:=first_seg;
  3304. end;
  3305. in_settextbuf_file_x,
  3306. in_reset_typedfile,
  3307. in_rewrite_typedfile,
  3308. in_str_x_string,
  3309. in_val_x,
  3310. in_read_x,
  3311. in_readln_x,
  3312. in_write_x,
  3313. in_writeln_x :
  3314. begin
  3315. { should be handled by pass_typecheck }
  3316. internalerror(200108234);
  3317. end;
  3318. in_get_frame:
  3319. begin
  3320. result:=first_get_frame;
  3321. end;
  3322. in_get_caller_frame:
  3323. begin
  3324. expectloc:=LOC_REGISTER;
  3325. end;
  3326. in_get_caller_addr:
  3327. begin
  3328. expectloc:=LOC_REGISTER;
  3329. end;
  3330. in_prefetch_var:
  3331. begin
  3332. expectloc:=LOC_VOID;
  3333. end;
  3334. in_aligned_x,
  3335. in_unaligned_x:
  3336. begin
  3337. expectloc:=tcallparanode(left).left.expectloc;
  3338. end;
  3339. in_rol_x,
  3340. in_rol_x_y,
  3341. in_ror_x,
  3342. in_ror_x_y,
  3343. in_bsf_x,
  3344. in_bsr_x:
  3345. expectloc:=LOC_REGISTER;
  3346. in_sar_x,
  3347. in_sar_x_y:
  3348. result:=first_sar;
  3349. in_popcnt_x:
  3350. result:=first_popcnt;
  3351. in_new_x:
  3352. result:=first_new;
  3353. in_box_x:
  3354. result:=first_box;
  3355. in_unbox_x_y:
  3356. result:=first_unbox;
  3357. else
  3358. internalerror(89);
  3359. end;
  3360. end;
  3361. {$maxfpuregisters default}
  3362. function tinlinenode.docompare(p: tnode): boolean;
  3363. begin
  3364. docompare :=
  3365. inherited docompare(p) and
  3366. (inlinenumber = tinlinenode(p).inlinenumber);
  3367. end;
  3368. function tinlinenode.first_pi : tnode;
  3369. begin
  3370. result:=crealconstnode.create(getpi,pbestrealtype^);
  3371. end;
  3372. function tinlinenode.first_arctan_real : tnode;
  3373. begin
  3374. { create the call to the helper }
  3375. { on entry left node contains the parameter }
  3376. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3377. ccallparanode.create(left,nil));
  3378. left := nil;
  3379. end;
  3380. function tinlinenode.first_abs_real : tnode;
  3381. begin
  3382. { create the call to the helper }
  3383. { on entry left node contains the parameter }
  3384. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3385. ccallparanode.create(left,nil));
  3386. left := nil;
  3387. end;
  3388. function tinlinenode.first_sqr_real : tnode;
  3389. begin
  3390. {$ifndef cpufpemu}
  3391. { this procedure might be only used for cpus definining cpufpemu else
  3392. the optimizer might go into an endless loop when doing x*x -> changes }
  3393. internalerror(2011092401);
  3394. {$endif cpufpemu}
  3395. { create the call to the helper }
  3396. { on entry left node contains the parameter }
  3397. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3398. ccallparanode.create(left,nil)),resultdef);
  3399. left := nil;
  3400. end;
  3401. function tinlinenode.first_sqrt_real : tnode;
  3402. begin
  3403. { create the call to the helper }
  3404. { on entry left node contains the parameter }
  3405. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3406. ccallparanode.create(left,nil)),resultdef);
  3407. left := nil;
  3408. end;
  3409. function tinlinenode.first_ln_real : tnode;
  3410. begin
  3411. { create the call to the helper }
  3412. { on entry left node contains the parameter }
  3413. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3414. ccallparanode.create(left,nil));
  3415. left := nil;
  3416. end;
  3417. function tinlinenode.first_cos_real : tnode;
  3418. begin
  3419. { create the call to the helper }
  3420. { on entry left node contains the parameter }
  3421. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3422. ccallparanode.create(left,nil));
  3423. left := nil;
  3424. end;
  3425. function tinlinenode.first_sin_real : tnode;
  3426. begin
  3427. { create the call to the helper }
  3428. { on entry left node contains the parameter }
  3429. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3430. ccallparanode.create(left,nil));
  3431. left := nil;
  3432. end;
  3433. function tinlinenode.first_exp_real : tnode;
  3434. begin
  3435. { create the call to the helper }
  3436. { on entry left node contains the parameter }
  3437. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3438. left := nil;
  3439. end;
  3440. function tinlinenode.first_int_real : tnode;
  3441. begin
  3442. { create the call to the helper }
  3443. { on entry left node contains the parameter }
  3444. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3445. left := nil;
  3446. end;
  3447. function tinlinenode.first_frac_real : tnode;
  3448. begin
  3449. { create the call to the helper }
  3450. { on entry left node contains the parameter }
  3451. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3452. left := nil;
  3453. end;
  3454. function tinlinenode.first_round_real : tnode;
  3455. begin
  3456. { create the call to the helper }
  3457. { on entry left node contains the parameter }
  3458. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3459. left := nil;
  3460. end;
  3461. function tinlinenode.first_trunc_real : tnode;
  3462. begin
  3463. { create the call to the helper }
  3464. { on entry left node contains the parameter }
  3465. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3466. left := nil;
  3467. end;
  3468. function tinlinenode.first_abs_long : tnode;
  3469. begin
  3470. expectloc:=LOC_REGISTER;
  3471. result:=nil;
  3472. end;
  3473. function tinlinenode.first_IncludeExclude: tnode;
  3474. begin
  3475. result:=nil;
  3476. expectloc:=LOC_VOID;
  3477. end;
  3478. function tinlinenode.first_get_frame: tnode;
  3479. begin
  3480. include(current_procinfo.flags,pi_needs_stackframe);
  3481. expectloc:=LOC_CREGISTER;
  3482. result:=nil;
  3483. end;
  3484. function tinlinenode.first_setlength: tnode;
  3485. var
  3486. paras : tnode;
  3487. npara,
  3488. ppn : tcallparanode;
  3489. dims,
  3490. counter : integer;
  3491. isarray : boolean;
  3492. destppn : tnode;
  3493. newstatement : tstatementnode;
  3494. temp : ttempcreatenode;
  3495. newblock : tnode;
  3496. begin
  3497. paras:=left;
  3498. ppn:=tcallparanode(paras);
  3499. dims:=0;
  3500. while assigned(ppn.right) do
  3501. begin
  3502. inc(dims);
  3503. ppn:=tcallparanode(ppn.right);
  3504. end;
  3505. destppn:=ppn.left;
  3506. isarray:=is_dynamic_array(destppn.resultdef);
  3507. { first param must be a string or dynamic array ...}
  3508. if isarray then
  3509. begin
  3510. { create statements with call initialize the arguments and
  3511. call fpc_dynarr_setlength }
  3512. newblock:=internalstatements(newstatement);
  3513. { get temp for array of lengths }
  3514. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3515. addstatement(newstatement,temp);
  3516. { load array of lengths }
  3517. ppn:=tcallparanode(paras);
  3518. counter:=dims-1;
  3519. while assigned(ppn.right) do
  3520. begin
  3521. addstatement(newstatement,cassignmentnode.create(
  3522. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3523. ppn.left));
  3524. ppn.left:=nil;
  3525. dec(counter);
  3526. ppn:=tcallparanode(ppn.right);
  3527. end;
  3528. { destppn is also reused }
  3529. ppn.left:=nil;
  3530. { create call to fpc_dynarr_setlength }
  3531. npara:=ccallparanode.create(caddrnode.create_internal
  3532. (ctemprefnode.create(temp)),
  3533. ccallparanode.create(cordconstnode.create
  3534. (dims,sinttype,true),
  3535. ccallparanode.create(caddrnode.create_internal
  3536. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3537. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3538. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3539. addstatement(newstatement,ctempdeletenode.create(temp));
  3540. end
  3541. else if is_ansistring(destppn.resultdef) then
  3542. begin
  3543. newblock:=ccallnode.createintern(
  3544. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3545. ccallparanode.create(
  3546. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3547. paras
  3548. )
  3549. );
  3550. { we reused the parameters, make sure we don't release them }
  3551. left:=nil;
  3552. end
  3553. else
  3554. begin
  3555. { we can reuse the supplied parameters }
  3556. newblock:=ccallnode.createintern(
  3557. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3558. { we reused the parameters, make sure we don't release them }
  3559. left:=nil;
  3560. end;
  3561. result:=newblock;
  3562. end;
  3563. function tinlinenode.first_copy: tnode;
  3564. var
  3565. lowppn,
  3566. highppn,
  3567. npara,
  3568. paras : tnode;
  3569. ppn : tcallparanode;
  3570. paradef : tdef;
  3571. counter : integer;
  3572. begin
  3573. { determine copy function to use based on the first argument,
  3574. also count the number of arguments in this loop }
  3575. counter:=1;
  3576. paras:=left;
  3577. ppn:=tcallparanode(paras);
  3578. while assigned(ppn.right) do
  3579. begin
  3580. inc(counter);
  3581. ppn:=tcallparanode(ppn.right);
  3582. end;
  3583. paradef:=ppn.left.resultdef;
  3584. { fill up third parameter }
  3585. if counter=2 then
  3586. begin
  3587. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3588. counter:=3;
  3589. end;
  3590. if is_ansistring(resultdef) then
  3591. { keep the specific kind of ansistringdef as result }
  3592. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3593. else if is_widestring(resultdef) then
  3594. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3595. else if is_unicodestring(resultdef) then
  3596. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3597. { can't check for resultdef = cansichartype, because resultdef=
  3598. cshortstringtype here }
  3599. else if is_char(paradef) then
  3600. result:=ccallnode.createintern('fpc_char_copy',paras)
  3601. else if is_dynamic_array(resultdef) then
  3602. begin
  3603. { create statements with call }
  3604. case counter of
  3605. 1:
  3606. begin
  3607. { copy the whole array using [0..high(sizeint)] range }
  3608. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3609. lowppn:=cordconstnode.create(0,sinttype,false);
  3610. end;
  3611. 3:
  3612. begin
  3613. highppn:=tcallparanode(paras).left.getcopy;
  3614. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3615. end;
  3616. else
  3617. internalerror(2012100701);
  3618. end;
  3619. { create call to fpc_dynarray_copy }
  3620. npara:=ccallparanode.create(highppn,
  3621. ccallparanode.create(lowppn,
  3622. ccallparanode.create(caddrnode.create_internal
  3623. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3624. ccallparanode.create
  3625. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3626. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3627. ppn.left:=nil;
  3628. paras.free;
  3629. end
  3630. else
  3631. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3632. { parameters are reused }
  3633. left:=nil;
  3634. end;
  3635. function tinlinenode.first_new: tnode;
  3636. var
  3637. newstatement : tstatementnode;
  3638. newblock : tblocknode;
  3639. temp : ttempcreatenode;
  3640. para : tcallparanode;
  3641. begin
  3642. { create statements with call to getmem+initialize }
  3643. newblock:=internalstatements(newstatement);
  3644. { create temp for result }
  3645. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  3646. addstatement(newstatement,temp);
  3647. { create call to fpc_getmem }
  3648. para := ccallparanode.create(cordconstnode.create
  3649. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  3650. addstatement(newstatement,cassignmentnode.create(
  3651. ctemprefnode.create(temp),
  3652. ccallnode.createintern('fpc_getmem',para)));
  3653. { create call to fpc_initialize }
  3654. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  3655. begin
  3656. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  3657. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  3658. ccallparanode.create(ctemprefnode.create
  3659. (temp),nil));
  3660. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  3661. end;
  3662. { the last statement should return the value as
  3663. location and type, this is done be referencing the
  3664. temp and converting it first from a persistent temp to
  3665. normal temp }
  3666. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3667. addstatement(newstatement,ctemprefnode.create(temp));
  3668. result:=newblock;
  3669. end;
  3670. function tinlinenode.first_length: tnode;
  3671. begin
  3672. result:=nil;
  3673. if is_shortstring(left.resultdef) then
  3674. expectloc:=left.expectloc
  3675. else
  3676. begin
  3677. { ansi/wide string }
  3678. expectloc:=LOC_REGISTER;
  3679. end;
  3680. end;
  3681. function tinlinenode.first_assigned: tnode;
  3682. begin
  3683. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  3684. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  3685. include(result.flags,nf_load_procvar);
  3686. tcallparanode(left).left:=nil;
  3687. end;
  3688. function tinlinenode.first_assert: tnode;
  3689. var
  3690. paras: tcallparanode;
  3691. begin
  3692. paras:=tcallparanode(tcallparanode(left).right);
  3693. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3694. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3695. {$ifdef SUPPORT_GET_FRAME}
  3696. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3697. {$else}
  3698. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3699. {$endif}
  3700. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3701. ccallnode.createintern('fpc_assert',paras),nil);
  3702. tcallparanode(left).left:=nil;
  3703. tcallparanode(left).right:=nil;
  3704. end;
  3705. function tinlinenode.first_popcnt: tnode;
  3706. var
  3707. suffix : string;
  3708. begin
  3709. case torddef(left.resultdef).ordtype of
  3710. u8bit: suffix:='byte';
  3711. u16bit: suffix:='word';
  3712. u32bit: suffix:='dword';
  3713. u64bit: suffix:='qword';
  3714. else
  3715. internalerror(2012082601);
  3716. end;
  3717. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  3718. left:=nil;
  3719. end;
  3720. function tinlinenode.typecheck_seg: tnode;
  3721. begin
  3722. if target_info.system in systems_managed_vm then
  3723. message(parser_e_feature_unsupported_for_vm);
  3724. set_varstate(left,vs_read,[]);
  3725. result:=cordconstnode.create(0,s32inttype,false);
  3726. end;
  3727. function tinlinenode.first_seg: tnode;
  3728. begin
  3729. internalerror(200104046);
  3730. result:=nil;
  3731. end;
  3732. function tinlinenode.first_sar: tnode;
  3733. begin
  3734. result:=nil;
  3735. expectloc:=LOC_REGISTER;
  3736. {$ifndef cpu64bitalu}
  3737. if is_64bitint(resultdef) then
  3738. begin
  3739. if (inlinenumber=in_sar_x) then
  3740. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  3741. ccallparanode.create(left,nil));
  3742. result:=ccallnode.createintern('fpc_sarint64',left);
  3743. left:=nil;
  3744. end;
  3745. {$endif cpu64bitalu}
  3746. end;
  3747. function tinlinenode.handle_box: tnode;
  3748. begin
  3749. result:=nil;
  3750. if not assigned(left) or
  3751. assigned(tcallparanode(left).right) then
  3752. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3753. resultdef:=class_tobject;
  3754. end;
  3755. function tinlinenode.handle_unbox: tnode;
  3756. begin
  3757. result:=nil;
  3758. if not assigned(left) or
  3759. not assigned(tcallparanode(left).right) or
  3760. assigned(tcallparanode(tcallparanode(left).right).right) then
  3761. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3762. if tcallparanode(left).left.nodetype<>typen then
  3763. internalerror(2011071701);
  3764. ttypenode(tcallparanode(left).left).allowed:=true;
  3765. resultdef:=tcallparanode(left).left.resultdef;
  3766. end;
  3767. function tinlinenode.first_pack_unpack: tnode;
  3768. var
  3769. loopstatement : tstatementnode;
  3770. loop : tblocknode;
  3771. loopvar : ttempcreatenode;
  3772. tempnode,
  3773. source,
  3774. target,
  3775. index,
  3776. unpackednode,
  3777. packednode,
  3778. sourcevecindex,
  3779. targetvecindex,
  3780. loopbody : tnode;
  3781. temprangedef : tdef;
  3782. ulorange,
  3783. uhirange,
  3784. plorange,
  3785. phirange : TConstExprInt;
  3786. begin
  3787. { transform into a for loop which assigns the data of the (un)packed }
  3788. { array to the other one }
  3789. source := left;
  3790. if (inlinenumber = in_unpack_x_y_z) then
  3791. begin
  3792. target := tcallparanode(source).right;
  3793. index := tcallparanode(target).right;
  3794. packednode := tcallparanode(source).left;
  3795. unpackednode := tcallparanode(target).left;
  3796. end
  3797. else
  3798. begin
  3799. index := tcallparanode(source).right;
  3800. target := tcallparanode(index).right;
  3801. packednode := tcallparanode(target).left;
  3802. unpackednode := tcallparanode(source).left;
  3803. end;
  3804. source := tcallparanode(source).left;
  3805. target := tcallparanode(target).left;
  3806. index := tcallparanode(index).left;
  3807. loop := internalstatements(loopstatement);
  3808. loopvar := ctempcreatenode.create(
  3809. tarraydef(packednode.resultdef).rangedef,
  3810. tarraydef(packednode.resultdef).rangedef.size,
  3811. tt_persistent,true);
  3812. addstatement(loopstatement,loopvar);
  3813. { For range checking: we have to convert to an integer type (in case the index type }
  3814. { is an enum), add the index and loop variable together, convert the result }
  3815. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3816. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3817. { errors }
  3818. temprangedef:=nil;
  3819. getrange(unpackednode.resultdef,ulorange,uhirange);
  3820. getrange(packednode.resultdef,plorange,phirange);
  3821. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3822. sourcevecindex := ctemprefnode.create(loopvar);
  3823. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3824. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3825. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3826. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3827. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3828. if (inlinenumber = in_pack_x_y_z) then
  3829. begin
  3830. { swap source and target vec indices }
  3831. tempnode := sourcevecindex;
  3832. sourcevecindex := targetvecindex;
  3833. targetvecindex := tempnode;
  3834. end;
  3835. { create the assignment in the loop body }
  3836. loopbody :=
  3837. cassignmentnode.create(
  3838. cvecnode.create(target.getcopy,targetvecindex),
  3839. cvecnode.create(source.getcopy,sourcevecindex)
  3840. );
  3841. { create the actual for loop }
  3842. tempnode := cfornode.create(
  3843. ctemprefnode.create(loopvar),
  3844. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3845. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3846. loopbody,
  3847. false);
  3848. addstatement(loopstatement,tempnode);
  3849. { free the loop counter }
  3850. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3851. result := loop;
  3852. end;
  3853. end.