ninl.pas 109 KB

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