ninl.pas 100 KB

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