ninl.pas 95 KB

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