ninl.pas 110 KB

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