ninl.pas 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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:=genintconstnode(abs(vl));
  1161. end;
  1162. in_const_sqr :
  1163. begin
  1164. if isreal then
  1165. hp:=crealconstnode.create(sqr(vr),pbestrealtype^)
  1166. else
  1167. hp:=genintconstnode(sqr(vl));
  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. if (trealconstnode(hp).value_real=double(MathInf)) and
  1239. ((cs_check_range in aktlocalswitches) or
  1240. (cs_check_overflow in aktlocalswitches)) then
  1241. begin
  1242. result:=crealconstnode.create(0,pbestrealtype^);
  1243. CGMessage(parser_e_range_check_error);
  1244. end;
  1245. end;
  1246. in_const_ln :
  1247. begin
  1248. if isreal then
  1249. hp:=handle_ln_const(vr)
  1250. else
  1251. hp:=handle_ln_const(vl)
  1252. end;
  1253. else
  1254. internalerror(88);
  1255. end;
  1256. end;
  1257. if hp=nil then
  1258. hp:=tnode.create(errorn);
  1259. result:=hp;
  1260. goto myexit;
  1261. end
  1262. else
  1263. begin
  1264. case inlinenumber of
  1265. in_lo_long,
  1266. in_hi_long,
  1267. in_lo_qword,
  1268. in_hi_qword,
  1269. in_lo_word,
  1270. in_hi_word :
  1271. begin
  1272. { give warning for incompatibility with tp and delphi }
  1273. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1274. ((m_tp7 in aktmodeswitches) or
  1275. (m_delphi in aktmodeswitches)) then
  1276. CGMessage(type_w_maybe_wrong_hi_lo);
  1277. { constant folding }
  1278. if left.nodetype=ordconstn then
  1279. begin
  1280. case inlinenumber of
  1281. in_lo_word :
  1282. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype,true);
  1283. in_hi_word :
  1284. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype,true);
  1285. in_lo_long :
  1286. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype,true);
  1287. in_hi_long :
  1288. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype,true);
  1289. in_lo_qword :
  1290. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype,true);
  1291. in_hi_qword :
  1292. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype,true);
  1293. end;
  1294. result:=hp;
  1295. goto myexit;
  1296. end;
  1297. set_varstate(left,vs_used,true);
  1298. if not is_integer(left.resulttype.def) then
  1299. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename);
  1300. case inlinenumber of
  1301. in_lo_word,
  1302. in_hi_word :
  1303. resulttype:=u8inttype;
  1304. in_lo_long,
  1305. in_hi_long :
  1306. resulttype:=u16inttype;
  1307. in_lo_qword,
  1308. in_hi_qword :
  1309. resulttype:=u32inttype;
  1310. end;
  1311. end;
  1312. in_sizeof_x:
  1313. begin
  1314. set_varstate(left,vs_used,false);
  1315. if paramanager.push_high_param(vs_value,left.resulttype.def,current_procinfo.procdef.proccalloption) then
  1316. begin
  1317. hightree:=load_high_value_node(tvarsym(tloadnode(left).symtableentry));
  1318. if assigned(hightree) then
  1319. begin
  1320. hp:=caddnode.create(addn,hightree,
  1321. cordconstnode.create(1,sinttype,false));
  1322. if (left.resulttype.def.deftype=arraydef) and
  1323. (tarraydef(left.resulttype.def).elesize<>1) then
  1324. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1325. left.resulttype.def).elesize,sinttype,true));
  1326. result:=hp;
  1327. end;
  1328. end
  1329. else
  1330. resulttype:=sinttype;
  1331. end;
  1332. in_typeof_x:
  1333. begin
  1334. set_varstate(left,vs_used,false);
  1335. resulttype:=voidpointertype;
  1336. end;
  1337. in_ord_x:
  1338. begin
  1339. if (left.nodetype=ordconstn) then
  1340. begin
  1341. hp:=cordconstnode.create(
  1342. tordconstnode(left).value,sinttype,true);
  1343. result:=hp;
  1344. goto myexit;
  1345. end;
  1346. set_varstate(left,vs_used,true);
  1347. case left.resulttype.def.deftype of
  1348. orddef :
  1349. begin
  1350. case torddef(left.resulttype.def).typ of
  1351. bool8bit,
  1352. uchar:
  1353. begin
  1354. { change to byte() }
  1355. hp:=ctypeconvnode.create_explicit(left,u8inttype);
  1356. left:=nil;
  1357. result:=hp;
  1358. end;
  1359. bool16bit,
  1360. uwidechar :
  1361. begin
  1362. { change to word() }
  1363. hp:=ctypeconvnode.create_explicit(left,u16inttype);
  1364. left:=nil;
  1365. result:=hp;
  1366. end;
  1367. bool32bit :
  1368. begin
  1369. { change to dword() }
  1370. hp:=ctypeconvnode.create_explicit(left,u32inttype);
  1371. left:=nil;
  1372. result:=hp;
  1373. end;
  1374. uvoid :
  1375. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.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_explicit(left,s32inttype);
  1388. left:=nil;
  1389. result:=hp;
  1390. end;
  1391. pointerdef :
  1392. begin
  1393. if m_mac in aktmodeswitches then
  1394. begin
  1395. hp:=ctypeconvnode.create_explicit(left,ptrinttype);
  1396. left:=nil;
  1397. result:=hp;
  1398. end
  1399. else
  1400. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1401. end
  1402. else
  1403. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1404. end;
  1405. end;
  1406. in_chr_byte:
  1407. begin
  1408. { convert to explicit char() }
  1409. set_varstate(left,vs_used,true);
  1410. hp:=ctypeconvnode.create_explicit(left,cchartype);
  1411. left:=nil;
  1412. result:=hp;
  1413. end;
  1414. in_length_x:
  1415. begin
  1416. set_varstate(left,vs_used,true);
  1417. case left.resulttype.def.deftype of
  1418. stringdef :
  1419. begin
  1420. { we don't need string convertions here }
  1421. if (left.nodetype=typeconvn) and
  1422. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1423. begin
  1424. hp:=ttypeconvnode(left).left;
  1425. ttypeconvnode(left).left:=nil;
  1426. left.free;
  1427. left:=hp;
  1428. end;
  1429. { evaluates length of constant strings direct }
  1430. if (left.nodetype=stringconstn) then
  1431. begin
  1432. hp:=cordconstnode.create(
  1433. tstringconstnode(left).len,s32inttype,true);
  1434. result:=hp;
  1435. goto myexit;
  1436. end;
  1437. end;
  1438. orddef :
  1439. begin
  1440. { length of char is one allways }
  1441. if is_char(left.resulttype.def) or
  1442. is_widechar(left.resulttype.def) then
  1443. begin
  1444. hp:=cordconstnode.create(1,s32inttype,false);
  1445. result:=hp;
  1446. goto myexit;
  1447. end
  1448. else
  1449. CGMessage(type_e_mismatch);
  1450. end;
  1451. pointerdef :
  1452. begin
  1453. if is_pchar(left.resulttype.def) then
  1454. begin
  1455. hp := ccallparanode.create(left,nil);
  1456. result := ccallnode.createintern('fpc_pchar_length',hp);
  1457. { make sure the left node doesn't get disposed, since it's }
  1458. { reused in the new node (JM) }
  1459. left:=nil;
  1460. goto myexit;
  1461. end
  1462. else if is_pwidechar(left.resulttype.def) then
  1463. begin
  1464. hp := ccallparanode.create(left,nil);
  1465. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1466. { make sure the left node doesn't get disposed, since it's }
  1467. { reused in the new node (JM) }
  1468. left:=nil;
  1469. goto myexit;
  1470. end
  1471. else
  1472. CGMessage(type_e_mismatch);
  1473. end;
  1474. arraydef :
  1475. begin
  1476. if is_open_array(left.resulttype.def) or
  1477. is_array_of_const(left.resulttype.def) then
  1478. begin
  1479. hightree:=load_high_value_node(tvarsym(tloadnode(left).symtableentry));
  1480. if assigned(hightree) then
  1481. begin
  1482. hp:=caddnode.create(addn,hightree,
  1483. cordconstnode.create(1,s32inttype,false));
  1484. result:=hp;
  1485. end;
  1486. goto myexit;
  1487. end
  1488. else
  1489. if not is_dynamic_array(left.resulttype.def) then
  1490. begin
  1491. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1492. tarraydef(left.resulttype.def).lowrange+1,
  1493. s32inttype,true);
  1494. result:=hp;
  1495. goto myexit;
  1496. end
  1497. else
  1498. begin
  1499. hp := ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil);
  1500. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1501. { make sure the left node doesn't get disposed, since it's }
  1502. { reused in the new node (JM) }
  1503. left:=nil;
  1504. goto myexit;
  1505. end;
  1506. end;
  1507. else
  1508. CGMessage(type_e_mismatch);
  1509. end;
  1510. { shortstring return an 8 bit value as the length
  1511. is the first byte of the string }
  1512. if is_shortstring(left.resulttype.def) then
  1513. resulttype:=u8inttype
  1514. else
  1515. resulttype:=sinttype;
  1516. end;
  1517. in_typeinfo_x:
  1518. begin
  1519. set_varstate(left,vs_used,true);
  1520. resulttype:=voidpointertype;
  1521. end;
  1522. in_assigned_x:
  1523. begin
  1524. { the parser has already made sure the expression is valid }
  1525. { handle constant expressions }
  1526. if is_constnode(tcallparanode(left).left) or
  1527. (tcallparanode(left).left.nodetype = pointerconstn) then
  1528. begin
  1529. { let an add node figure it out }
  1530. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1531. tcallparanode(left).left := nil;
  1532. { free left, because otherwise some code at 'myexit' tries }
  1533. { to run get_paratype for it, which crashes since left.left }
  1534. { is now nil }
  1535. left.free;
  1536. left := nil;
  1537. goto myexit;
  1538. end;
  1539. { otherwise handle separately, because there could be a procvar, which }
  1540. { is 2*sizeof(pointer), while we must only check the first pointer }
  1541. set_varstate(tcallparanode(left).left,vs_used,true);
  1542. resulttype:=booltype;
  1543. end;
  1544. in_ofs_x :
  1545. internalerror(2000101001);
  1546. in_seg_x :
  1547. begin
  1548. set_varstate(left,vs_used,false);
  1549. hp:=cordconstnode.create(0,s32inttype,false);
  1550. result:=hp;
  1551. goto myexit;
  1552. end;
  1553. in_pred_x,
  1554. in_succ_x:
  1555. begin
  1556. set_varstate(left,vs_used,true);
  1557. resulttype:=left.resulttype;
  1558. if not is_ordinal(resulttype.def) then
  1559. CGMessage(type_e_ordinal_expr_expected)
  1560. else
  1561. begin
  1562. if (resulttype.def.deftype=enumdef) and
  1563. (tenumdef(resulttype.def).has_jumps) then
  1564. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1565. end;
  1566. { only if the result is an enum do we do range checking }
  1567. if (resulttype.def.deftype=enumdef) then
  1568. checkrange := true
  1569. else
  1570. checkrange := false;
  1571. { do constant folding after check for jumps }
  1572. if left.nodetype=ordconstn then
  1573. begin
  1574. if inlinenumber=in_succ_x then
  1575. hp:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype,checkrange)
  1576. else
  1577. hp:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype,checkrange);
  1578. result:=hp;
  1579. end;
  1580. end;
  1581. in_initialize_x,
  1582. in_finalize_x,
  1583. in_setlength_x:
  1584. begin
  1585. { inlined from pinline }
  1586. internalerror(200204231);
  1587. end;
  1588. in_inc_x,
  1589. in_dec_x:
  1590. begin
  1591. resulttype:=voidtype;
  1592. if assigned(left) then
  1593. begin
  1594. { first param must be var }
  1595. valid_for_var(tcallparanode(left).left);
  1596. set_varstate(tcallparanode(left).left,vs_used,true);
  1597. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1598. is_ordinal(left.resulttype.def) or
  1599. is_currency(left.resulttype.def) then
  1600. begin
  1601. { value of left gets changed -> must be unique }
  1602. set_unique(tcallparanode(left).left);
  1603. { two paras ? }
  1604. if assigned(tcallparanode(left).right) then
  1605. begin
  1606. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1607. inserttypeconv_explicit(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1608. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1609. CGMessage(parser_e_illegal_expression);
  1610. end;
  1611. end
  1612. else
  1613. CGMessage(type_e_ordinal_expr_expected);
  1614. end
  1615. else
  1616. CGMessage(type_e_mismatch);
  1617. end;
  1618. in_read_x,
  1619. in_readln_x,
  1620. in_write_x,
  1621. in_writeln_x :
  1622. begin
  1623. result := handle_read_write;
  1624. end;
  1625. in_settextbuf_file_x :
  1626. begin
  1627. resulttype:=voidtype;
  1628. { now we know the type of buffer }
  1629. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1630. hp:=ccallparanode.create(cordconstnode.create(
  1631. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1632. hp:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil,[]);
  1633. left:=nil;
  1634. result:=hp;
  1635. end;
  1636. { the firstpass of the arg has been done in firstcalln ? }
  1637. in_reset_typedfile,
  1638. in_rewrite_typedfile :
  1639. begin
  1640. result := handle_reset_rewrite_typed;
  1641. end;
  1642. in_str_x_string :
  1643. begin
  1644. result := handle_str;
  1645. end;
  1646. in_val_x :
  1647. begin
  1648. result := handle_val;
  1649. end;
  1650. in_include_x_y,
  1651. in_exclude_x_y:
  1652. begin
  1653. resulttype:=voidtype;
  1654. { the parser already checks whether we have two (and exectly two) }
  1655. { parameters (JM) }
  1656. { first param must be var }
  1657. valid_for_var(tcallparanode(left).left);
  1658. set_varstate(tcallparanode(left).left,vs_used,true);
  1659. { check type }
  1660. if (left.resulttype.def.deftype=setdef) then
  1661. begin
  1662. { insert a type conversion }
  1663. { to the type of the set elements }
  1664. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1665. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1666. tsetdef(left.resulttype.def).elementtype);
  1667. end
  1668. else
  1669. CGMessage(type_e_mismatch);
  1670. end;
  1671. in_low_x,
  1672. in_high_x:
  1673. begin
  1674. set_varstate(left,vs_used,false);
  1675. case left.resulttype.def.deftype of
  1676. orddef,
  1677. enumdef:
  1678. begin
  1679. result:=do_lowhigh(left.resulttype);
  1680. end;
  1681. setdef:
  1682. begin
  1683. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1684. end;
  1685. arraydef:
  1686. begin
  1687. if inlinenumber=in_low_x then
  1688. begin
  1689. result:=cordconstnode.create(tarraydef(
  1690. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1691. end
  1692. else
  1693. begin
  1694. if is_open_array(left.resulttype.def) or
  1695. is_array_of_const(left.resulttype.def) then
  1696. begin
  1697. result:=load_high_value_node(tvarsym(tloadnode(left).symtableentry));
  1698. end
  1699. else
  1700. if is_dynamic_array(left.resulttype.def) then
  1701. begin
  1702. { can't use inserttypeconv because we need }
  1703. { an explicit type conversion (JM) }
  1704. hp := ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil);
  1705. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1706. { make sure the left node doesn't get disposed, since it's }
  1707. { reused in the new node (JM) }
  1708. left:=nil;
  1709. end
  1710. else
  1711. begin
  1712. result:=cordconstnode.create(tarraydef(
  1713. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1714. end;
  1715. end;
  1716. end;
  1717. stringdef:
  1718. begin
  1719. if inlinenumber=in_low_x then
  1720. begin
  1721. result:=cordconstnode.create(0,u8inttype,false);
  1722. end
  1723. else
  1724. begin
  1725. if is_open_string(left.resulttype.def) then
  1726. result:=load_high_value_node(tvarsym(tloadnode(left).symtableentry))
  1727. else
  1728. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1729. end;
  1730. end;
  1731. else
  1732. CGMessage(type_e_mismatch);
  1733. end;
  1734. end;
  1735. in_pi:
  1736. begin
  1737. if block_type=bt_const then
  1738. setconstrealvalue(pi)
  1739. else
  1740. resulttype:=pbestrealtype^;
  1741. end;
  1742. in_cos_extended :
  1743. begin
  1744. if left.nodetype in [ordconstn,realconstn] then
  1745. setconstrealvalue(cos(getconstrealvalue))
  1746. else
  1747. begin
  1748. set_varstate(left,vs_used,true);
  1749. inserttypeconv(left,pbestrealtype^);
  1750. resulttype:=pbestrealtype^;
  1751. end;
  1752. end;
  1753. in_sin_extended :
  1754. begin
  1755. if left.nodetype in [ordconstn,realconstn] then
  1756. setconstrealvalue(sin(getconstrealvalue))
  1757. else
  1758. begin
  1759. set_varstate(left,vs_used,true);
  1760. inserttypeconv(left,pbestrealtype^);
  1761. resulttype:=pbestrealtype^;
  1762. end;
  1763. end;
  1764. in_arctan_extended :
  1765. begin
  1766. if left.nodetype in [ordconstn,realconstn] then
  1767. setconstrealvalue(arctan(getconstrealvalue))
  1768. else
  1769. begin
  1770. set_varstate(left,vs_used,true);
  1771. inserttypeconv(left,pbestrealtype^);
  1772. resulttype:=pbestrealtype^;
  1773. end;
  1774. end;
  1775. in_abs_extended :
  1776. begin
  1777. if left.nodetype in [ordconstn,realconstn] then
  1778. setconstrealvalue(abs(getconstrealvalue))
  1779. else
  1780. begin
  1781. set_varstate(left,vs_used,true);
  1782. inserttypeconv(left,pbestrealtype^);
  1783. resulttype:=pbestrealtype^;
  1784. end;
  1785. end;
  1786. in_sqr_extended :
  1787. begin
  1788. if left.nodetype in [ordconstn,realconstn] then
  1789. setconstrealvalue(sqr(getconstrealvalue))
  1790. else
  1791. begin
  1792. set_varstate(left,vs_used,true);
  1793. inserttypeconv(left,pbestrealtype^);
  1794. resulttype:=pbestrealtype^;
  1795. end;
  1796. end;
  1797. in_sqrt_extended :
  1798. begin
  1799. if left.nodetype in [ordconstn,realconstn] then
  1800. begin
  1801. vr:=getconstrealvalue;
  1802. if vr<0.0 then
  1803. result:=handle_sqrt_const(vr)
  1804. else
  1805. setconstrealvalue(sqrt(vr));
  1806. end
  1807. else
  1808. begin
  1809. set_varstate(left,vs_used,true);
  1810. inserttypeconv(left,pbestrealtype^);
  1811. resulttype:=pbestrealtype^;
  1812. end;
  1813. end;
  1814. in_ln_extended :
  1815. begin
  1816. if left.nodetype in [ordconstn,realconstn] then
  1817. begin
  1818. vr:=getconstrealvalue;
  1819. if vr<=0.0 then
  1820. result:=handle_ln_const(vr)
  1821. else
  1822. setconstrealvalue(ln(vr));
  1823. end
  1824. else
  1825. begin
  1826. set_varstate(left,vs_used,true);
  1827. inserttypeconv(left,pbestrealtype^);
  1828. resulttype:=pbestrealtype^;
  1829. end;
  1830. end;
  1831. {$ifdef SUPPORT_MMX}
  1832. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1833. begin
  1834. end;
  1835. {$endif SUPPORT_MMX}
  1836. in_prefetch_var:
  1837. begin
  1838. resulttype:=voidtype;
  1839. end;
  1840. in_assert_x_y :
  1841. begin
  1842. resulttype:=voidtype;
  1843. if assigned(left) then
  1844. begin
  1845. set_varstate(tcallparanode(left).left,vs_used,true);
  1846. { check type }
  1847. if is_boolean(left.resulttype.def) then
  1848. begin
  1849. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1850. { must always be a string }
  1851. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1852. end
  1853. else
  1854. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1855. end
  1856. else
  1857. CGMessage(type_e_mismatch);
  1858. { We've checked the whole statement for correctness, now we
  1859. can remove it if assertions are off }
  1860. if not(cs_do_assertion in aktlocalswitches) then
  1861. begin
  1862. { we need a valid node, so insert a nothingn }
  1863. result:=cnothingnode.create;
  1864. end
  1865. else
  1866. include(current_procinfo.flags,pi_do_call);
  1867. end;
  1868. else
  1869. internalerror(8);
  1870. end;
  1871. end;
  1872. myexit:
  1873. { Run get_paratype again to update maybe inserted typeconvs }
  1874. if not codegenerror then
  1875. begin
  1876. if assigned(left) and
  1877. (left.nodetype=callparan) then
  1878. tcallparanode(left).get_paratype;
  1879. end;
  1880. dec(parsing_para_level);
  1881. end;
  1882. function tinlinenode.pass_1 : tnode;
  1883. var
  1884. hp,hpp : tnode;
  1885. shiftconst: longint;
  1886. tempnode: ttempcreatenode;
  1887. newstatement: tstatementnode;
  1888. newblock: tblocknode;
  1889. begin
  1890. result:=nil;
  1891. { if we handle writeln; left contains no valid address }
  1892. if assigned(left) then
  1893. begin
  1894. if left.nodetype=callparan then
  1895. tcallparanode(left).firstcallparan
  1896. else
  1897. firstpass(left);
  1898. left_max;
  1899. end;
  1900. inc(parsing_para_level);
  1901. { intern const should already be handled }
  1902. if nf_inlineconst in flags then
  1903. internalerror(200104044);
  1904. case inlinenumber of
  1905. in_lo_qword,
  1906. in_hi_qword,
  1907. in_lo_long,
  1908. in_hi_long,
  1909. in_lo_word,
  1910. in_hi_word:
  1911. begin
  1912. shiftconst := 0;
  1913. case inlinenumber of
  1914. in_hi_qword:
  1915. shiftconst := 32;
  1916. in_hi_long:
  1917. shiftconst := 16;
  1918. in_hi_word:
  1919. shiftconst := 8;
  1920. end;
  1921. if shiftconst <> 0 then
  1922. result := ctypeconvnode.create_explicit(cshlshrnode.create(shrn,left,
  1923. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1924. else
  1925. result := ctypeconvnode.create_explicit(left,resulttype);
  1926. left := nil;
  1927. firstpass(result);
  1928. end;
  1929. in_sizeof_x:
  1930. begin
  1931. if registersint<1 then
  1932. registersint:=1;
  1933. expectloc:=LOC_REGISTER;
  1934. end;
  1935. in_typeof_x:
  1936. begin
  1937. if registersint<1 then
  1938. registersint:=1;
  1939. expectloc:=LOC_REGISTER;
  1940. end;
  1941. in_length_x:
  1942. begin
  1943. if is_shortstring(left.resulttype.def) then
  1944. expectloc:=left.expectloc
  1945. else
  1946. begin
  1947. { ansi/wide string }
  1948. if registersint<1 then
  1949. registersint:=1;
  1950. expectloc:=LOC_REGISTER;
  1951. end;
  1952. end;
  1953. in_typeinfo_x:
  1954. begin
  1955. expectloc:=LOC_REGISTER;
  1956. registersint:=1;
  1957. end;
  1958. in_assigned_x:
  1959. begin
  1960. expectloc := LOC_JUMP;
  1961. registersint:=1;
  1962. end;
  1963. in_pred_x,
  1964. in_succ_x:
  1965. begin
  1966. if is_64bit(resulttype.def) then
  1967. begin
  1968. if (registersint<2) then
  1969. registersint:=2
  1970. end
  1971. else
  1972. begin
  1973. if (registersint<1) then
  1974. registersint:=1;
  1975. end;
  1976. expectloc:=LOC_REGISTER;
  1977. end;
  1978. in_setlength_x,
  1979. in_initialize_x,
  1980. in_finalize_x:
  1981. begin
  1982. expectloc:=LOC_VOID;
  1983. end;
  1984. in_inc_x,
  1985. in_dec_x:
  1986. begin
  1987. expectloc:=LOC_VOID;
  1988. { check type }
  1989. if
  1990. {$ifndef cpu64bit}
  1991. is_64bit(left.resulttype.def) or
  1992. {$endif cpu64bit}
  1993. { range/overflow checking doesn't work properly }
  1994. { with the inc/dec code that's generated (JM) }
  1995. (
  1996. (left.resulttype.def.deftype = orddef) and
  1997. not(is_char(left.resulttype.def)) and
  1998. not(is_boolean(left.resulttype.def)) and
  1999. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  2000. ) then
  2001. { convert to simple add (JM) }
  2002. begin
  2003. newblock := internalstatements(newstatement);
  2004. { extra parameter? }
  2005. if assigned(tcallparanode(left).right) then
  2006. begin
  2007. { Yes, use for add node }
  2008. hpp := tcallparanode(tcallparanode(left).right).left;
  2009. tcallparanode(tcallparanode(left).right).left := nil;
  2010. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2011. CGMessage(parser_e_illegal_expression);
  2012. end
  2013. else
  2014. { no, create constant 1 }
  2015. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  2016. { make sure we don't call functions part of the left node twice (and generally }
  2017. { optimize the code generation) }
  2018. if node_complexity(tcallparanode(left).left) > 1 then
  2019. begin
  2020. tempnode := ctempcreatenode.create_reg(voidpointertype,voidpointertype.def.size,tt_persistent);
  2021. addstatement(newstatement,tempnode);
  2022. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2023. caddrnode.create(tcallparanode(left).left.getcopy)));
  2024. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2025. inserttypeconv_explicit(hp,tcallparanode(left).left.resulttype);
  2026. end
  2027. else
  2028. begin
  2029. hp := tcallparanode(left).left.getcopy;
  2030. tempnode := nil;
  2031. end;
  2032. { addition/substraction depending on inc/dec }
  2033. if inlinenumber = in_inc_x then
  2034. hpp := caddnode.create(addn,hp,hpp)
  2035. else
  2036. hpp := caddnode.create(subn,hp,hpp);
  2037. { assign result of addition }
  2038. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  2039. { deallocate the temp }
  2040. if assigned(tempnode) then
  2041. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2042. { firstpass it }
  2043. firstpass(newblock);
  2044. { return new node }
  2045. result := newblock;
  2046. end
  2047. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  2048. is_ordinal(left.resulttype.def) then
  2049. begin
  2050. { two paras ? }
  2051. if assigned(tcallparanode(left).right) then
  2052. begin
  2053. { need we an additional register ? }
  2054. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2055. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2056. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2057. inc(registersint);
  2058. { do we need an additional register to restore the first parameter? }
  2059. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2060. inc(registersint);
  2061. end;
  2062. end;
  2063. end;
  2064. in_include_x_y,
  2065. in_exclude_x_y:
  2066. begin
  2067. expectloc:=LOC_VOID;
  2068. registersint:=left.registersint;
  2069. registersfpu:=left.registersfpu;
  2070. {$ifdef SUPPORT_MMX}
  2071. registersmmx:=left.registersmmx;
  2072. {$endif SUPPORT_MMX}
  2073. end;
  2074. in_cos_extended:
  2075. begin
  2076. result:= first_cos_real;
  2077. end;
  2078. in_sin_extended:
  2079. begin
  2080. result := first_sin_real;
  2081. end;
  2082. in_arctan_extended:
  2083. begin
  2084. result := first_arctan_real;
  2085. end;
  2086. in_pi:
  2087. begin
  2088. result := first_pi;
  2089. end;
  2090. in_abs_extended:
  2091. begin
  2092. result := first_abs_real;
  2093. end;
  2094. in_sqr_extended:
  2095. begin
  2096. result := first_sqr_real;
  2097. end;
  2098. in_sqrt_extended:
  2099. begin
  2100. result := first_sqrt_real;
  2101. end;
  2102. in_ln_extended:
  2103. begin
  2104. result := first_ln_real;
  2105. end;
  2106. {$ifdef SUPPORT_MMX}
  2107. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2108. begin
  2109. end;
  2110. {$endif SUPPORT_MMX}
  2111. in_assert_x_y :
  2112. begin
  2113. expectloc:=LOC_VOID;
  2114. registersint:=left.registersint;
  2115. registersfpu:=left.registersfpu;
  2116. {$ifdef SUPPORT_MMX}
  2117. registersmmx:=left.registersmmx;
  2118. {$endif SUPPORT_MMX}
  2119. end;
  2120. in_low_x,
  2121. in_high_x:
  2122. internalerror(200104047);
  2123. in_ord_x,
  2124. in_chr_byte:
  2125. begin
  2126. { should not happend as it's converted to typeconv }
  2127. internalerror(200104045);
  2128. end;
  2129. in_ofs_x :
  2130. internalerror(2000101001);
  2131. in_seg_x :
  2132. internalerror(200104046);
  2133. in_settextbuf_file_x,
  2134. in_reset_typedfile,
  2135. in_rewrite_typedfile,
  2136. in_str_x_string,
  2137. in_val_x,
  2138. in_read_x,
  2139. in_readln_x,
  2140. in_write_x,
  2141. in_writeln_x :
  2142. begin
  2143. { should be handled by det_resulttype }
  2144. internalerror(200108234);
  2145. end;
  2146. in_prefetch_var:
  2147. begin
  2148. expectloc:=LOC_VOID;
  2149. end;
  2150. else
  2151. internalerror(8);
  2152. end;
  2153. dec(parsing_para_level);
  2154. end;
  2155. {$ifdef fpc}
  2156. {$maxfpuregisters default}
  2157. {$endif fpc}
  2158. function tinlinenode.docompare(p: tnode): boolean;
  2159. begin
  2160. docompare :=
  2161. inherited docompare(p) and
  2162. (inlinenumber = tinlinenode(p).inlinenumber);
  2163. end;
  2164. function tinlinenode.first_pi : tnode;
  2165. begin
  2166. result := crealconstnode.create(pi,pbestrealtype^);
  2167. end;
  2168. function tinlinenode.first_arctan_real : tnode;
  2169. begin
  2170. { create the call to the helper }
  2171. { on entry left node contains the parameter }
  2172. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2173. ccallparanode.create(left,nil));
  2174. left := nil;
  2175. end;
  2176. function tinlinenode.first_abs_real : tnode;
  2177. begin
  2178. { create the call to the helper }
  2179. { on entry left node contains the parameter }
  2180. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2181. ccallparanode.create(left,nil));
  2182. left := nil;
  2183. end;
  2184. function tinlinenode.first_sqr_real : tnode;
  2185. begin
  2186. { create the call to the helper }
  2187. { on entry left node contains the parameter }
  2188. first_sqr_real := ccallnode.createintern('fpc_sqr_real',
  2189. ccallparanode.create(left,nil));
  2190. left := nil;
  2191. end;
  2192. function tinlinenode.first_sqrt_real : tnode;
  2193. begin
  2194. { create the call to the helper }
  2195. { on entry left node contains the parameter }
  2196. first_sqrt_real := ccallnode.createintern('fpc_sqrt_real',
  2197. ccallparanode.create(left,nil));
  2198. left := nil;
  2199. end;
  2200. function tinlinenode.first_ln_real : tnode;
  2201. begin
  2202. { create the call to the helper }
  2203. { on entry left node contains the parameter }
  2204. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2205. ccallparanode.create(left,nil));
  2206. left := nil;
  2207. end;
  2208. function tinlinenode.first_cos_real : tnode;
  2209. begin
  2210. { create the call to the helper }
  2211. { on entry left node contains the parameter }
  2212. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2213. ccallparanode.create(left,nil));
  2214. left := nil;
  2215. end;
  2216. function tinlinenode.first_sin_real : tnode;
  2217. begin
  2218. { create the call to the helper }
  2219. { on entry left node contains the parameter }
  2220. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2221. ccallparanode.create(left,nil));
  2222. left := nil;
  2223. end;
  2224. begin
  2225. cinlinenode:=tinlinenode;
  2226. end.
  2227. {
  2228. $Log$
  2229. Revision 1.143 2004-08-25 15:56:35 peter
  2230. * fix sqr() and abs() constant range check errors
  2231. Revision 1.142 2004/08/08 16:00:56 florian
  2232. * constant floating point assignments etc. are now overflow checked
  2233. if Q+ or R+ is turned on
  2234. Revision 1.141 2004/07/15 19:55:39 jonas
  2235. + (incomplete) node_complexity function to assess the complexity of a
  2236. tree
  2237. + support for inlining value and const parameters at the node level
  2238. (all procedures without local variables and without formal parameters
  2239. can now be inlined at the node level)
  2240. Revision 1.140 2004/07/14 21:40:52 olle
  2241. + added Ord(pointer) for macpas
  2242. Revision 1.139 2004/07/14 14:38:35 jonas
  2243. * fix for web bug 3210
  2244. Revision 1.138 2004/06/20 08:55:29 florian
  2245. * logs truncated
  2246. Revision 1.137 2004/06/18 15:16:46 peter
  2247. * remove obsolete cardinal() typecasts
  2248. Revision 1.136 2004/06/16 20:07:08 florian
  2249. * dwarf branch merged
  2250. Revision 1.135 2004/05/28 21:15:20 peter
  2251. * inc(x,y) makes y always of type x to prevent 64bit operations
  2252. when x is a u32bit and y is signed
  2253. Revision 1.134 2004/05/23 18:28:41 peter
  2254. * methodpointer is loaded into a temp when it was a calln
  2255. Revision 1.133.2.9 2004/05/03 16:49:00 peter
  2256. * sizeof fixed
  2257. }