ninl.pas 144 KB

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