ninl.pas 94 KB

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