ninl.pas 95 KB

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