ninl.pas 141 KB

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