ninl.pas 108 KB

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