ninl.pas 98 KB

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