ninl.pas 130 KB

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