ninl.pas 108 KB

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