ninl.pas 91 KB

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