ninl.pas 141 KB

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