ninl.pas 97 KB

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