ninl.pas 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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,types,
  47. pass_1,
  48. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,
  49. cpubase,tgcpu,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_' + lower(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+lower(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 + lower(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. function tinlinenode.det_resulttype:tnode;
  882. function do_lowhigh(const t:ttype) : tnode;
  883. var
  884. v : tconstexprint;
  885. enum : tenumsym;
  886. hp : tnode;
  887. begin
  888. case t.def.deftype of
  889. orddef:
  890. begin
  891. if inlinenumber=in_low_x then
  892. v:=torddef(t.def).low
  893. else
  894. v:=torddef(t.def).high;
  895. { low/high of torddef are longints, so we need special }
  896. { handling for cardinal and 64bit types (JM) }
  897. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  898. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  899. shl twice (PFV) }
  900. if is_signed(t.def) and
  901. is_64bitint(t.def) then
  902. if (inlinenumber=in_low_x) then
  903. v := int64($80000000) shl 32
  904. else
  905. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  906. else
  907. if is_64bitint(t.def) then
  908. { we have to use a dirty trick for high(qword), }
  909. { because it's bigger than high(tconstexprint) (JM) }
  910. v := 0
  911. else
  912. if not is_signed(t.def) then
  913. v := cardinal(v);
  914. hp:=cordconstnode.create(v,t);
  915. resulttypepass(hp);
  916. { fix high(qword) }
  917. if not is_signed(t.def) and
  918. is_64bitint(t.def) and
  919. (inlinenumber = in_high_x) then
  920. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  921. do_lowhigh:=hp;
  922. end;
  923. enumdef:
  924. begin
  925. enum:=tenumsym(tenumdef(t.def).firstenum);
  926. v:=tenumdef(t.def).maxval;
  927. if inlinenumber=in_high_x then
  928. while assigned(enum) and (enum.value <> v) do
  929. enum:=enum.nextenum;
  930. if not assigned(enum) then
  931. internalerror(309993)
  932. else
  933. hp:=genenumnode(enum);
  934. do_lowhigh:=hp;
  935. end;
  936. else
  937. internalerror(87);
  938. end;
  939. end;
  940. function getconstrealvalue : bestreal;
  941. begin
  942. case left.nodetype of
  943. ordconstn:
  944. getconstrealvalue:=tordconstnode(left).value;
  945. realconstn:
  946. getconstrealvalue:=trealconstnode(left).value_real;
  947. else
  948. internalerror(309992);
  949. end;
  950. end;
  951. procedure setconstrealvalue(r : bestreal);
  952. begin
  953. result:=crealconstnode.create(r,pbestrealtype^);
  954. end;
  955. var
  956. counter : longint;
  957. ppn : tcallparanode;
  958. dummycoll : tparaitem;
  959. vl,vl2 : longint;
  960. vr : bestreal;
  961. hp : tnode;
  962. srsym : tsym;
  963. def : tdef;
  964. isreal : boolean;
  965. label
  966. myexit;
  967. begin
  968. result:=nil;
  969. { if we handle writeln; left contains no valid address }
  970. if assigned(left) then
  971. begin
  972. if left.nodetype=callparan then
  973. tcallparanode(left).get_paratype
  974. else
  975. resulttypepass(left);
  976. end;
  977. inc(parsing_para_level);
  978. { handle intern constant functions in separate case }
  979. if nf_inlineconst in flags then
  980. begin
  981. { no parameters? }
  982. if not assigned(left) then
  983. begin
  984. case inlinenumber of
  985. in_const_pi :
  986. hp:=crealconstnode.create(pi,pbestrealtype^);
  987. else
  988. internalerror(89);
  989. end;
  990. end
  991. else
  992. begin
  993. vl:=0;
  994. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  995. vr:=0;
  996. isreal:=false;
  997. case left.nodetype of
  998. realconstn :
  999. begin
  1000. isreal:=true;
  1001. vr:=trealconstnode(left).value_real;
  1002. end;
  1003. ordconstn :
  1004. vl:=tordconstnode(left).value;
  1005. callparan :
  1006. begin
  1007. { both exists, else it was not generated }
  1008. vl:=tordconstnode(tcallparanode(left).left).value;
  1009. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1010. end;
  1011. else
  1012. CGMessage(cg_e_illegal_expression);
  1013. end;
  1014. case inlinenumber of
  1015. in_const_trunc :
  1016. begin
  1017. if isreal then
  1018. begin
  1019. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  1020. begin
  1021. CGMessage(parser_e_range_check_error);
  1022. hp:=cordconstnode.create(1,s32bittype)
  1023. end
  1024. else
  1025. hp:=cordconstnode.create(trunc(vr),s32bittype)
  1026. end
  1027. else
  1028. hp:=cordconstnode.create(trunc(vl),s32bittype);
  1029. end;
  1030. in_const_round :
  1031. begin
  1032. if isreal then
  1033. begin
  1034. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  1035. begin
  1036. CGMessage(parser_e_range_check_error);
  1037. hp:=cordconstnode.create(1,s32bittype)
  1038. end
  1039. else
  1040. hp:=cordconstnode.create(round(vr),s32bittype)
  1041. end
  1042. else
  1043. hp:=cordconstnode.create(round(vl),s32bittype);
  1044. end;
  1045. in_const_frac :
  1046. begin
  1047. if isreal then
  1048. hp:=crealconstnode.create(frac(vr),pbestrealtype^)
  1049. else
  1050. hp:=crealconstnode.create(frac(vl),pbestrealtype^);
  1051. end;
  1052. in_const_int :
  1053. begin
  1054. if isreal then
  1055. hp:=crealconstnode.create(int(vr),pbestrealtype^)
  1056. else
  1057. hp:=crealconstnode.create(int(vl),pbestrealtype^);
  1058. end;
  1059. in_const_abs :
  1060. begin
  1061. if isreal then
  1062. hp:=crealconstnode.create(abs(vr),pbestrealtype^)
  1063. else
  1064. hp:=cordconstnode.create(abs(vl),left.resulttype);
  1065. end;
  1066. in_const_sqr :
  1067. begin
  1068. if isreal then
  1069. hp:=crealconstnode.create(sqr(vr),pbestrealtype^)
  1070. else
  1071. hp:=cordconstnode.create(sqr(vl),left.resulttype);
  1072. end;
  1073. in_const_odd :
  1074. begin
  1075. if isreal then
  1076. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1077. else
  1078. hp:=cordconstnode.create(byte(odd(vl)),booltype);
  1079. end;
  1080. in_const_swap_word :
  1081. begin
  1082. if isreal then
  1083. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1084. else
  1085. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype);
  1086. end;
  1087. in_const_swap_long :
  1088. begin
  1089. if isreal then
  1090. CGMessage(type_e_mismatch)
  1091. else
  1092. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype);
  1093. end;
  1094. in_const_ptr :
  1095. begin
  1096. if isreal then
  1097. CGMessage(type_e_mismatch)
  1098. else
  1099. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1100. end;
  1101. in_const_sqrt :
  1102. begin
  1103. if isreal then
  1104. begin
  1105. if vr<0.0 then
  1106. CGMessage(type_e_wrong_math_argument)
  1107. else
  1108. hp:=crealconstnode.create(sqrt(vr),pbestrealtype^)
  1109. end
  1110. else
  1111. begin
  1112. if vl<0 then
  1113. CGMessage(type_e_wrong_math_argument)
  1114. else
  1115. hp:=crealconstnode.create(sqrt(vl),pbestrealtype^);
  1116. end;
  1117. end;
  1118. in_const_arctan :
  1119. begin
  1120. if isreal then
  1121. hp:=crealconstnode.create(arctan(vr),pbestrealtype^)
  1122. else
  1123. hp:=crealconstnode.create(arctan(vl),pbestrealtype^);
  1124. end;
  1125. in_const_cos :
  1126. begin
  1127. if isreal then
  1128. hp:=crealconstnode.create(cos(vr),pbestrealtype^)
  1129. else
  1130. hp:=crealconstnode.create(cos(vl),pbestrealtype^);
  1131. end;
  1132. in_const_sin :
  1133. begin
  1134. if isreal then
  1135. hp:=crealconstnode.create(sin(vr),pbestrealtype^)
  1136. else
  1137. hp:=crealconstnode.create(sin(vl),pbestrealtype^);
  1138. end;
  1139. in_const_exp :
  1140. begin
  1141. if isreal then
  1142. hp:=crealconstnode.create(exp(vr),pbestrealtype^)
  1143. else
  1144. hp:=crealconstnode.create(exp(vl),pbestrealtype^);
  1145. end;
  1146. in_const_ln :
  1147. begin
  1148. if isreal then
  1149. begin
  1150. if vr<=0.0 then
  1151. CGMessage(type_e_wrong_math_argument)
  1152. else
  1153. hp:=crealconstnode.create(ln(vr),pbestrealtype^)
  1154. end
  1155. else
  1156. begin
  1157. if vl<=0 then
  1158. CGMessage(type_e_wrong_math_argument)
  1159. else
  1160. hp:=crealconstnode.create(ln(vl),pbestrealtype^);
  1161. end;
  1162. end;
  1163. else
  1164. internalerror(88);
  1165. end;
  1166. end;
  1167. if hp=nil then
  1168. hp:=tnode.create(errorn);
  1169. result:=hp;
  1170. goto myexit;
  1171. end
  1172. else
  1173. begin
  1174. case inlinenumber of
  1175. in_lo_long,
  1176. in_hi_long,
  1177. in_lo_qword,
  1178. in_hi_qword,
  1179. in_lo_word,
  1180. in_hi_word :
  1181. begin
  1182. { give warning for incompatibility with tp and delphi }
  1183. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1184. ((m_tp in aktmodeswitches) or
  1185. (m_delphi in aktmodeswitches)) then
  1186. CGMessage(type_w_maybe_wrong_hi_lo);
  1187. { constant folding }
  1188. if left.nodetype=ordconstn then
  1189. begin
  1190. case inlinenumber of
  1191. in_lo_word :
  1192. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype);
  1193. in_hi_word :
  1194. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype);
  1195. in_lo_long :
  1196. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype);
  1197. in_hi_long :
  1198. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype);
  1199. in_lo_qword :
  1200. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype);
  1201. in_hi_qword :
  1202. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype);
  1203. end;
  1204. result:=hp;
  1205. goto myexit;
  1206. end;
  1207. set_varstate(left,true);
  1208. if not is_integer(left.resulttype.def) then
  1209. CGMessage(type_e_mismatch);
  1210. case inlinenumber of
  1211. in_lo_word,
  1212. in_hi_word :
  1213. resulttype:=u8bittype;
  1214. in_lo_long,
  1215. in_hi_long :
  1216. resulttype:=u16bittype;
  1217. in_lo_qword,
  1218. in_hi_qword :
  1219. resulttype:=u32bittype;
  1220. end;
  1221. end;
  1222. in_sizeof_x:
  1223. begin
  1224. set_varstate(left,false);
  1225. resulttype:=s32bittype;
  1226. end;
  1227. in_typeof_x:
  1228. begin
  1229. set_varstate(left,false);
  1230. resulttype:=voidpointertype;
  1231. end;
  1232. in_ord_x:
  1233. begin
  1234. if (left.nodetype=ordconstn) then
  1235. begin
  1236. hp:=cordconstnode.create(tordconstnode(left).value,s32bittype);
  1237. result:=hp;
  1238. goto myexit;
  1239. end;
  1240. set_varstate(left,true);
  1241. case left.resulttype.def.deftype of
  1242. orddef :
  1243. begin
  1244. case torddef(left.resulttype.def).typ of
  1245. bool8bit,
  1246. uchar:
  1247. begin
  1248. { change to byte() }
  1249. hp:=ctypeconvnode.create(left,u8bittype);
  1250. left:=nil;
  1251. include(hp.flags,nf_explizit);
  1252. result:=hp;
  1253. end;
  1254. bool16bit,
  1255. uwidechar :
  1256. begin
  1257. { change to word() }
  1258. hp:=ctypeconvnode.create(left,u16bittype);
  1259. left:=nil;
  1260. include(hp.flags,nf_explizit);
  1261. result:=hp;
  1262. end;
  1263. bool32bit :
  1264. begin
  1265. { change to dword() }
  1266. hp:=ctypeconvnode.create(left,u32bittype);
  1267. left:=nil;
  1268. include(hp.flags,nf_explizit);
  1269. result:=hp;
  1270. end;
  1271. uvoid :
  1272. CGMessage(type_e_mismatch)
  1273. else
  1274. begin
  1275. { all other orddef need no transformation }
  1276. hp:=left;
  1277. left:=nil;
  1278. result:=hp;
  1279. end;
  1280. end;
  1281. end;
  1282. enumdef :
  1283. begin
  1284. hp:=ctypeconvnode.create(left,s32bittype);
  1285. left:=nil;
  1286. include(hp.flags,nf_explizit);
  1287. result:=hp;
  1288. end;
  1289. else
  1290. CGMessage(type_e_mismatch);
  1291. end;
  1292. end;
  1293. in_chr_byte:
  1294. begin
  1295. { convert to explicit char() }
  1296. set_varstate(left,true);
  1297. hp:=ctypeconvnode.create(left,cchartype);
  1298. include(hp.flags,nf_explizit);
  1299. left:=nil;
  1300. result:=hp;
  1301. end;
  1302. in_length_x:
  1303. begin
  1304. set_varstate(left,true);
  1305. case left.resulttype.def.deftype of
  1306. stringdef :
  1307. begin
  1308. { we don't need string convertions here }
  1309. if (left.nodetype=typeconvn) and
  1310. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1311. begin
  1312. hp:=ttypeconvnode(left).left;
  1313. ttypeconvnode(left).left:=nil;
  1314. left.free;
  1315. left:=hp;
  1316. end;
  1317. { evaluates length of constant strings direct }
  1318. if (left.nodetype=stringconstn) then
  1319. begin
  1320. hp:=cordconstnode.create(tstringconstnode(left).len,s32bittype);
  1321. result:=hp;
  1322. goto myexit;
  1323. end;
  1324. end;
  1325. orddef :
  1326. begin
  1327. { length of char is one allways }
  1328. if is_char(left.resulttype.def) or
  1329. is_widechar(left.resulttype.def) then
  1330. begin
  1331. hp:=cordconstnode.create(1,s32bittype);
  1332. result:=hp;
  1333. goto myexit;
  1334. end
  1335. else
  1336. CGMessage(type_e_mismatch);
  1337. end;
  1338. arraydef :
  1339. begin
  1340. if is_open_array(left.resulttype.def) or
  1341. is_array_of_const(left.resulttype.def) then
  1342. begin
  1343. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1344. hp:=caddnode.create(addn,cloadnode.create(tvarsym(srsym),tloadnode(left).symtable),
  1345. cordconstnode.create(1,s32bittype));
  1346. result:=hp;
  1347. goto myexit;
  1348. end
  1349. else
  1350. if not is_dynamic_array(left.resulttype.def) then
  1351. begin
  1352. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1353. tarraydef(left.resulttype.def).lowrange+1,
  1354. s32bittype);
  1355. result:=hp;
  1356. goto myexit;
  1357. end
  1358. else
  1359. begin
  1360. { can't use inserttypeconv because we need }
  1361. { an explicit type conversion (JM) }
  1362. hp := ctypeconvnode.create(left,voidpointertype);
  1363. hp.toggleflag(nf_explizit);
  1364. hp := ccallparanode.create(hp,nil);
  1365. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1366. { make sure the left node doesn't get disposed, since it's }
  1367. { reused in the new node (JM) }
  1368. left:=nil;
  1369. resulttypepass(result);
  1370. exit;
  1371. end;
  1372. end;
  1373. else
  1374. CGMessage(type_e_mismatch);
  1375. end;
  1376. { shortstring return an 8 bit value as the length
  1377. is the first byte of the string }
  1378. if is_shortstring(left.resulttype.def) then
  1379. resulttype:=u8bittype
  1380. else
  1381. resulttype:=s32bittype;
  1382. end;
  1383. in_typeinfo_x:
  1384. begin
  1385. set_varstate(left,true);
  1386. resulttype:=voidpointertype;
  1387. end;
  1388. in_assigned_x:
  1389. begin
  1390. set_varstate(left,true);
  1391. resulttype:=booltype;
  1392. end;
  1393. in_ofs_x :
  1394. internalerror(2000101001);
  1395. in_seg_x :
  1396. begin
  1397. set_varstate(left,false);
  1398. hp:=cordconstnode.create(0,s32bittype);
  1399. result:=hp;
  1400. goto myexit;
  1401. end;
  1402. in_pred_x,
  1403. in_succ_x:
  1404. begin
  1405. set_varstate(left,true);
  1406. resulttype:=left.resulttype;
  1407. if not is_ordinal(resulttype.def) then
  1408. CGMessage(type_e_ordinal_expr_expected)
  1409. else
  1410. begin
  1411. if (resulttype.def.deftype=enumdef) and
  1412. (tenumdef(resulttype.def).has_jumps) then
  1413. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1414. end;
  1415. { do constant folding after check for jumps }
  1416. if left.nodetype=ordconstn then
  1417. begin
  1418. if inlinenumber=in_succ_x then
  1419. hp:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype)
  1420. else
  1421. hp:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype);
  1422. result:=hp;
  1423. end;
  1424. end;
  1425. in_setlength_x:
  1426. begin
  1427. resulttype:=voidtype;
  1428. if assigned(left) then
  1429. begin
  1430. ppn:=tcallparanode(left);
  1431. counter:=0;
  1432. { check type }
  1433. while assigned(ppn.right) do
  1434. begin
  1435. set_varstate(ppn.left,true);
  1436. inserttypeconv(ppn.left,s32bittype);
  1437. inc(counter);
  1438. ppn:=tcallparanode(ppn.right);
  1439. end;
  1440. { last param must be var }
  1441. valid_for_var(ppn.left);
  1442. set_varstate(ppn.left,false);
  1443. { first param must be a string or dynamic array ...}
  1444. if not((ppn.left.resulttype.def.deftype=stringdef) or
  1445. (is_dynamic_array(ppn.left.resulttype.def))) then
  1446. CGMessage(type_e_mismatch);
  1447. { only dynamic arrays accept more dimensions }
  1448. if (counter>1) then
  1449. if (not(is_dynamic_array(ppn.left.resulttype.def))) then
  1450. CGMessage(type_e_mismatch)
  1451. else
  1452. { check if the amount of dimensions is valid }
  1453. begin
  1454. def := tarraydef(ppn.left.resulttype.def).elementtype.def;
  1455. while counter > 1 do
  1456. begin
  1457. if not(is_dynamic_array(def)) then
  1458. begin
  1459. CGMessage(parser_e_wrong_parameter_size);
  1460. break;
  1461. end;
  1462. dec(counter);
  1463. def := tarraydef(def).elementtype.def;
  1464. end;
  1465. end;
  1466. { convert shortstrings to openstring parameters }
  1467. { (generate the hightree) (JM) }
  1468. if (ppn.left.resulttype.def.deftype = stringdef) and
  1469. (tstringdef(ppn.left.resulttype.def).string_typ =
  1470. st_shortstring) then
  1471. begin
  1472. dummycoll:=tparaitem.create;
  1473. dummycoll.paratyp:=vs_var;
  1474. dummycoll.paratype:=openshortstringtype;
  1475. tcallparanode(ppn).insert_typeconv(dummycoll,false);
  1476. dummycoll.destroy;
  1477. end;
  1478. end
  1479. else
  1480. CGMessage(type_e_mismatch);
  1481. end;
  1482. in_finalize_x:
  1483. begin
  1484. resulttype:=voidtype;
  1485. if assigned(left) and assigned(tcallparanode(left).left) then
  1486. begin
  1487. { first param must be var }
  1488. valid_for_var(tcallparanode(left).left);
  1489. set_varstate(tcallparanode(left).left,true);
  1490. { two parameters?, the last parameter must be a longint }
  1491. if assigned(tcallparanode(left).right) then
  1492. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  1493. end
  1494. else
  1495. CGMessage(type_e_mismatch);
  1496. end;
  1497. in_inc_x,
  1498. in_dec_x:
  1499. begin
  1500. resulttype:=voidtype;
  1501. if assigned(left) then
  1502. begin
  1503. set_varstate(left,true);
  1504. if codegenerror then
  1505. exit;
  1506. { first param must be var }
  1507. valid_for_var(tcallparanode(left).left);
  1508. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1509. is_ordinal(left.resulttype.def) then
  1510. begin
  1511. { two paras ? }
  1512. if assigned(tcallparanode(left).right) then
  1513. begin
  1514. { insert a type conversion }
  1515. { the second param is always longint }
  1516. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  1517. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1518. CGMessage(cg_e_illegal_expression);
  1519. end;
  1520. end
  1521. else
  1522. CGMessage(type_e_ordinal_expr_expected);
  1523. end
  1524. else
  1525. CGMessage(type_e_mismatch);
  1526. end;
  1527. in_read_x,
  1528. in_readln_x,
  1529. in_write_x,
  1530. in_writeln_x :
  1531. begin
  1532. result := handle_read_write;
  1533. end;
  1534. in_settextbuf_file_x :
  1535. begin
  1536. resulttype:=voidtype;
  1537. { now we know the type of buffer }
  1538. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1539. hp:=ccallparanode.create(cordconstnode.create(tcallparanode(left).left.resulttype.def.size,s32bittype),left);
  1540. hp:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil);
  1541. left:=nil;
  1542. result:=hp;
  1543. end;
  1544. { the firstpass of the arg has been done in firstcalln ? }
  1545. in_reset_typedfile,
  1546. in_rewrite_typedfile :
  1547. begin
  1548. result := handle_reset_rewrite_typed;
  1549. end;
  1550. in_str_x_string :
  1551. begin
  1552. result := handle_str;
  1553. end;
  1554. in_val_x :
  1555. begin
  1556. result := handle_val;
  1557. end;
  1558. in_include_x_y,
  1559. in_exclude_x_y:
  1560. begin
  1561. resulttype:=voidtype;
  1562. { the parser already checks whether we have two (and exectly two) }
  1563. { parameters (JM) }
  1564. set_varstate(left,true);
  1565. { first param must be var }
  1566. valid_for_var(tcallparanode(left).left);
  1567. { check type }
  1568. if (left.resulttype.def.deftype=setdef) then
  1569. begin
  1570. { insert a type conversion }
  1571. { to the type of the set elements }
  1572. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1573. tsetdef(left.resulttype.def).elementtype);
  1574. end
  1575. else
  1576. CGMessage(type_e_mismatch);
  1577. end;
  1578. in_low_x,
  1579. in_high_x:
  1580. begin
  1581. set_varstate(left,false);
  1582. case left.resulttype.def.deftype of
  1583. orddef,
  1584. enumdef:
  1585. begin
  1586. result:=do_lowhigh(left.resulttype);
  1587. end;
  1588. setdef:
  1589. begin
  1590. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1591. end;
  1592. arraydef:
  1593. begin
  1594. if inlinenumber=in_low_x then
  1595. begin
  1596. result:=cordconstnode.create(tarraydef(left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype);
  1597. end
  1598. else
  1599. begin
  1600. if is_open_array(left.resulttype.def) or
  1601. is_array_of_const(left.resulttype.def) then
  1602. begin
  1603. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1604. result:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1605. end
  1606. else
  1607. if is_dynamic_array(left.resulttype.def) then
  1608. begin
  1609. { can't use inserttypeconv because we need }
  1610. { an explicit type conversion (JM) }
  1611. hp := ctypeconvnode.create(left,voidpointertype);
  1612. hp.toggleflag(nf_explizit);
  1613. hp := ccallparanode.create(hp,nil);
  1614. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1615. { make sure the left node doesn't get disposed, since it's }
  1616. { reused in the new node (JM) }
  1617. left:=nil;
  1618. end
  1619. else
  1620. begin
  1621. result:=cordconstnode.create(tarraydef(left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype);
  1622. end;
  1623. end;
  1624. resulttypepass(result);
  1625. end;
  1626. stringdef:
  1627. begin
  1628. if inlinenumber=in_low_x then
  1629. begin
  1630. hp:=cordconstnode.create(0,u8bittype);
  1631. resulttypepass(hp);
  1632. result:=hp;
  1633. end
  1634. else
  1635. begin
  1636. if is_open_string(left.resulttype.def) then
  1637. begin
  1638. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1639. hp:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1640. resulttypepass(hp);
  1641. result:=hp;
  1642. end
  1643. else
  1644. begin
  1645. hp:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8bittype);
  1646. resulttypepass(hp);
  1647. result:=hp;
  1648. end;
  1649. end;
  1650. end;
  1651. else
  1652. CGMessage(type_e_mismatch);
  1653. end;
  1654. end;
  1655. in_pi:
  1656. begin
  1657. if block_type=bt_const then
  1658. setconstrealvalue(pi)
  1659. else
  1660. resulttype:=s80floattype;
  1661. end;
  1662. in_cos_extended :
  1663. begin
  1664. if left.nodetype in [ordconstn,realconstn] then
  1665. setconstrealvalue(cos(getconstrealvalue))
  1666. else
  1667. begin
  1668. set_varstate(left,true);
  1669. inserttypeconv(left,s80floattype);
  1670. resulttype:=s80floattype;
  1671. end;
  1672. end;
  1673. in_sin_extended :
  1674. begin
  1675. if left.nodetype in [ordconstn,realconstn] then
  1676. setconstrealvalue(sin(getconstrealvalue))
  1677. else
  1678. begin
  1679. set_varstate(left,true);
  1680. inserttypeconv(left,s80floattype);
  1681. resulttype:=s80floattype;
  1682. end;
  1683. end;
  1684. in_arctan_extended :
  1685. begin
  1686. if left.nodetype in [ordconstn,realconstn] then
  1687. setconstrealvalue(arctan(getconstrealvalue))
  1688. else
  1689. begin
  1690. set_varstate(left,true);
  1691. inserttypeconv(left,s80floattype);
  1692. resulttype:=s80floattype;
  1693. end;
  1694. end;
  1695. in_abs_extended :
  1696. begin
  1697. if left.nodetype in [ordconstn,realconstn] then
  1698. setconstrealvalue(abs(getconstrealvalue))
  1699. else
  1700. begin
  1701. set_varstate(left,true);
  1702. inserttypeconv(left,s80floattype);
  1703. resulttype:=s80floattype;
  1704. end;
  1705. end;
  1706. in_sqr_extended :
  1707. begin
  1708. if left.nodetype in [ordconstn,realconstn] then
  1709. setconstrealvalue(sqr(getconstrealvalue))
  1710. else
  1711. begin
  1712. set_varstate(left,true);
  1713. inserttypeconv(left,s80floattype);
  1714. resulttype:=s80floattype;
  1715. end;
  1716. end;
  1717. in_sqrt_extended :
  1718. begin
  1719. if left.nodetype in [ordconstn,realconstn] then
  1720. begin
  1721. vr:=getconstrealvalue;
  1722. if vr<0.0 then
  1723. begin
  1724. CGMessage(type_e_wrong_math_argument);
  1725. setconstrealvalue(0);
  1726. end
  1727. else
  1728. setconstrealvalue(sqrt(vr));
  1729. end
  1730. else
  1731. begin
  1732. set_varstate(left,true);
  1733. inserttypeconv(left,s80floattype);
  1734. resulttype:=s80floattype;
  1735. end;
  1736. end;
  1737. in_ln_extended :
  1738. begin
  1739. if left.nodetype in [ordconstn,realconstn] then
  1740. begin
  1741. vr:=getconstrealvalue;
  1742. if vr<=0.0 then
  1743. begin
  1744. CGMessage(type_e_wrong_math_argument);
  1745. setconstrealvalue(0);
  1746. end
  1747. else
  1748. setconstrealvalue(ln(vr));
  1749. end
  1750. else
  1751. begin
  1752. set_varstate(left,true);
  1753. inserttypeconv(left,s80floattype);
  1754. resulttype:=s80floattype;
  1755. end;
  1756. end;
  1757. {$ifdef SUPPORT_MMX}
  1758. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1759. begin
  1760. end;
  1761. {$endif SUPPORT_MMX}
  1762. in_assert_x_y :
  1763. begin
  1764. resulttype:=voidtype;
  1765. if assigned(left) then
  1766. begin
  1767. set_varstate(left,true);
  1768. { check type }
  1769. if is_boolean(left.resulttype.def) then
  1770. begin
  1771. { must always be a string }
  1772. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1773. end
  1774. else
  1775. CGMessage(type_e_mismatch);
  1776. end
  1777. else
  1778. CGMessage(type_e_mismatch);
  1779. end;
  1780. else
  1781. internalerror(8);
  1782. end;
  1783. end;
  1784. myexit:
  1785. { Run get_paratype again to update maybe inserted typeconvs }
  1786. if not codegenerror then
  1787. begin
  1788. if assigned(left) and
  1789. (left.nodetype=callparan) then
  1790. tcallparanode(left).get_paratype;
  1791. end;
  1792. dec(parsing_para_level);
  1793. end;
  1794. {$ifdef fpc}
  1795. {$maxfpuregisters 0}
  1796. {$endif fpc}
  1797. function tinlinenode.pass_1 : tnode;
  1798. var
  1799. srsym : tsym;
  1800. hp,hpp : tnode;
  1801. shiftconst: longint;
  1802. begin
  1803. result:=nil;
  1804. { if we handle writeln; left contains no valid address }
  1805. if assigned(left) then
  1806. begin
  1807. if left.nodetype=callparan then
  1808. tcallparanode(left).firstcallparan(nil,false)
  1809. else
  1810. firstpass(left);
  1811. left_max;
  1812. set_location(location,left.location);
  1813. end;
  1814. inc(parsing_para_level);
  1815. { intern const should already be handled }
  1816. if nf_inlineconst in flags then
  1817. internalerror(200104044);
  1818. case inlinenumber of
  1819. in_lo_qword,
  1820. in_hi_qword,
  1821. in_lo_long,
  1822. in_hi_long,
  1823. in_lo_word,
  1824. in_hi_word:
  1825. begin
  1826. shiftconst := 0;
  1827. case inlinenumber of
  1828. in_hi_qword:
  1829. shiftconst := 32;
  1830. in_hi_long:
  1831. shiftconst := 16;
  1832. in_hi_word:
  1833. shiftconst := 8;
  1834. end;
  1835. if shiftconst <> 0 then
  1836. result := ctypeconvnode.create(cshlshrnode.create(shrn,left,
  1837. cordconstnode.create(shiftconst,u32bittype)),resulttype)
  1838. else
  1839. result := ctypeconvnode.create(left,resulttype);
  1840. left := nil;
  1841. include(result.flags,nf_explizit);
  1842. firstpass(result);
  1843. end;
  1844. in_sizeof_x:
  1845. begin
  1846. if push_high_param(left.resulttype.def) then
  1847. begin
  1848. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1849. hp:=caddnode.create(addn,cloadnode.create(tvarsym(srsym),tloadnode(left).symtable),
  1850. cordconstnode.create(1,s32bittype));
  1851. if (left.resulttype.def.deftype=arraydef) and
  1852. (tarraydef(left.resulttype.def).elesize<>1) then
  1853. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(left.resulttype.def).elesize,s32bittype));
  1854. firstpass(hp);
  1855. result:=hp;
  1856. end
  1857. else
  1858. begin
  1859. if registers32<1 then
  1860. registers32:=1;
  1861. location.loc:=LOC_REGISTER;
  1862. end;
  1863. end;
  1864. in_typeof_x:
  1865. begin
  1866. if registers32<1 then
  1867. registers32:=1;
  1868. location.loc:=LOC_REGISTER;
  1869. end;
  1870. in_ord_x,
  1871. in_chr_byte:
  1872. begin
  1873. { should not happend as it's converted to typeconv }
  1874. internalerror(200104045);
  1875. end;
  1876. in_length_x:
  1877. begin
  1878. if is_shortstring(left.resulttype.def) then
  1879. location.loc:=LOC_REFERENCE
  1880. else
  1881. begin
  1882. { ansi/wide string }
  1883. if registers32<1 then
  1884. registers32:=1;
  1885. location.loc:=LOC_REGISTER;
  1886. end;
  1887. end;
  1888. in_typeinfo_x:
  1889. begin
  1890. location.loc:=LOC_REGISTER;
  1891. registers32:=1;
  1892. end;
  1893. in_assigned_x:
  1894. begin
  1895. location.loc:=LOC_FLAGS;
  1896. end;
  1897. in_ofs_x :
  1898. internalerror(2000101001);
  1899. in_seg_x :
  1900. internalerror(200104046);
  1901. in_pred_x,
  1902. in_succ_x:
  1903. begin
  1904. if is_64bitint(resulttype.def) then
  1905. begin
  1906. if (registers32<2) then
  1907. registers32:=2
  1908. end
  1909. else
  1910. begin
  1911. if (registers32<1) then
  1912. registers32:=1;
  1913. end;
  1914. location.loc:=LOC_REGISTER;
  1915. end;
  1916. in_setlength_x:
  1917. begin
  1918. end;
  1919. in_finalize_x:
  1920. begin
  1921. end;
  1922. in_inc_x,
  1923. in_dec_x:
  1924. begin
  1925. { check type }
  1926. if is_64bitint(left.resulttype.def) or
  1927. { range/overflow checking doesn't work properly }
  1928. { with the inc/dec code that's generated (JM) }
  1929. ((left.resulttype.def.deftype = orddef) and
  1930. not(is_char(left.resulttype.def)) and
  1931. not(is_boolean(left.resulttype.def)) and
  1932. (aktlocalswitches *
  1933. [cs_check_overflow,cs_check_range] <> [])) then
  1934. { convert to simple add (JM) }
  1935. begin
  1936. { extra parameter? }
  1937. if assigned(tcallparanode(left).right) then
  1938. begin
  1939. { Yes, use for add node }
  1940. hpp := tcallparanode(tcallparanode(left).right).left;
  1941. tcallparanode(tcallparanode(left).right).left := nil;
  1942. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1943. CGMessage(cg_e_illegal_expression);
  1944. end
  1945. else
  1946. { no, create constant 1 }
  1947. hpp := cordconstnode.create(1,s32bittype);
  1948. { addition/substraction depending on inc/dec }
  1949. if inlinenumber = in_inc_x then
  1950. hp := caddnode.create(addn,tcallparanode(left).left.getcopy,hpp)
  1951. else
  1952. hp := caddnode.create(subn,tcallparanode(left).left.getcopy,hpp);
  1953. { assign result of addition }
  1954. hpp := cassignmentnode.create(tcallparanode(left).left,hp);
  1955. tcallparanode(left).left := nil;
  1956. { firstpass it }
  1957. firstpass(hpp);
  1958. { return new node }
  1959. result := hpp;
  1960. end
  1961. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1962. is_ordinal(left.resulttype.def) then
  1963. begin
  1964. { two paras ? }
  1965. if assigned(tcallparanode(left).right) then
  1966. begin
  1967. { need we an additional register ? }
  1968. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  1969. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  1970. (tcallparanode(tcallparanode(left).right).left.registers32<=1) then
  1971. inc(registers32);
  1972. { do we need an additional register to restore the first parameter? }
  1973. if tcallparanode(tcallparanode(left).right).left.registers32>=registers32 then
  1974. inc(registers32);
  1975. end;
  1976. end;
  1977. end;
  1978. in_read_x,
  1979. in_readln_x,
  1980. in_write_x,
  1981. in_writeln_x :
  1982. begin
  1983. { should be handled by det_resulttype }
  1984. internalerror(200108234);
  1985. end;
  1986. in_settextbuf_file_x :
  1987. internalerror(200104262);
  1988. in_reset_typedfile,
  1989. in_rewrite_typedfile :
  1990. begin
  1991. { should already be removed in det_resulttype (JM) }
  1992. internalerror(200108236);
  1993. end;
  1994. in_str_x_string :
  1995. begin
  1996. { should already be removed in det_resulttype (JM) }
  1997. internalerror(200108235);
  1998. end;
  1999. in_val_x :
  2000. begin
  2001. { should already be removed in det_resulttype (JM) }
  2002. internalerror(200108242);
  2003. end;
  2004. in_include_x_y,
  2005. in_exclude_x_y:
  2006. begin
  2007. registers32:=left.registers32;
  2008. registersfpu:=left.registersfpu;
  2009. {$ifdef SUPPORT_MMX}
  2010. registersmmx:=left.registersmmx;
  2011. {$endif SUPPORT_MMX}
  2012. end;
  2013. in_low_x,
  2014. in_high_x:
  2015. internalerror(200104047);
  2016. in_cos_extended:
  2017. begin
  2018. location.loc:=LOC_FPU;
  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_sin_extended:
  2026. begin
  2027. location.loc:=LOC_FPU;
  2028. registers32:=left.registers32;
  2029. registersfpu:=max(left.registersfpu,1);
  2030. {$ifdef SUPPORT_MMX}
  2031. registersmmx:=left.registersmmx;
  2032. {$endif SUPPORT_MMX}
  2033. end;
  2034. in_arctan_extended:
  2035. begin
  2036. location.loc:=LOC_FPU;
  2037. registers32:=left.registers32;
  2038. registersfpu:=max(left.registersfpu,2);
  2039. {$ifdef SUPPORT_MMX}
  2040. registersmmx:=left.registersmmx;
  2041. {$endif SUPPORT_MMX}
  2042. end;
  2043. in_pi:
  2044. begin
  2045. location.loc:=LOC_FPU;
  2046. registersfpu:=1;
  2047. end;
  2048. in_abs_extended:
  2049. begin
  2050. location.loc:=LOC_FPU;
  2051. registers32:=left.registers32;
  2052. registersfpu:=max(left.registersfpu,1);
  2053. {$ifdef SUPPORT_MMX}
  2054. registersmmx:=left.registersmmx;
  2055. {$endif SUPPORT_MMX}
  2056. end;
  2057. in_sqr_extended:
  2058. begin
  2059. location.loc:=LOC_FPU;
  2060. registers32:=left.registers32;
  2061. registersfpu:=max(left.registersfpu,1);
  2062. {$ifdef SUPPORT_MMX}
  2063. registersmmx:=left.registersmmx;
  2064. {$endif SUPPORT_MMX}
  2065. end;
  2066. in_sqrt_extended:
  2067. begin
  2068. location.loc:=LOC_FPU;
  2069. registers32:=left.registers32;
  2070. registersfpu:=max(left.registersfpu,1);
  2071. {$ifdef SUPPORT_MMX}
  2072. registersmmx:=left.registersmmx;
  2073. {$endif SUPPORT_MMX}
  2074. end;
  2075. in_ln_extended:
  2076. begin
  2077. location.loc:=LOC_FPU;
  2078. registers32:=left.registers32;
  2079. registersfpu:=max(left.registersfpu,2);
  2080. {$ifdef SUPPORT_MMX}
  2081. registersmmx:=left.registersmmx;
  2082. {$endif SUPPORT_MMX}
  2083. end;
  2084. {$ifdef SUPPORT_MMX}
  2085. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2086. begin
  2087. end;
  2088. {$endif SUPPORT_MMX}
  2089. in_assert_x_y :
  2090. begin
  2091. { We've checked the whole statement for correctness, now we
  2092. can remove it if assertions are off }
  2093. if not(cs_do_assertion in aktlocalswitches) then
  2094. begin
  2095. { we need a valid node, so insert a nothingn }
  2096. result:=cnothingnode.create;
  2097. end
  2098. else
  2099. begin
  2100. registers32:=left.registers32;
  2101. registersfpu:=left.registersfpu;
  2102. {$ifdef SUPPORT_MMX}
  2103. registersmmx:=left.registersmmx;
  2104. {$endif SUPPORT_MMX}
  2105. end;
  2106. end;
  2107. else
  2108. internalerror(8);
  2109. end;
  2110. dec(parsing_para_level);
  2111. end;
  2112. {$ifdef fpc}
  2113. {$maxfpuregisters default}
  2114. {$endif fpc}
  2115. function tinlinenode.docompare(p: tnode): boolean;
  2116. begin
  2117. docompare :=
  2118. inherited docompare(p) and
  2119. (inlinenumber = tinlinenode(p).inlinenumber);
  2120. end;
  2121. begin
  2122. cinlinenode:=tinlinenode;
  2123. end.
  2124. {
  2125. $Log$
  2126. Revision 1.66 2001-12-10 14:26:22 jonas
  2127. - removed unnecessary resulttypepass call
  2128. Revision 1.65 2001/12/04 15:59:03 jonas
  2129. * converted lo/hi to processor independent code, generated code is the
  2130. same as before (when turning on the optimizer)
  2131. Revision 1.64 2001/12/03 14:21:34 jonas
  2132. * fixed web bug 1693 (dynarray support for length)
  2133. Revision 1.63 2001/10/24 16:17:36 jonas
  2134. * fixed web bug 1621 (write(typed_file,function_call) works again)
  2135. * allow write(typed_file,procvar_call) too (it was already allowed for
  2136. text file writes)
  2137. Revision 1.62 2001/09/30 16:16:28 jonas
  2138. - removed unused units form uses-clause and unused local vars
  2139. Revision 1.61 2001/09/24 16:09:55 jonas
  2140. * check if amount of dimensions passed to setlength for dynamic arrays
  2141. is correct
  2142. Revision 1.60 2001/09/24 11:35:55 jonas
  2143. * fix from Pavel V. Ozersk to accept multiple dimensions for setlength
  2144. and dynamical arrays
  2145. Revision 1.59 2001/09/17 21:29:12 peter
  2146. * merged netbsd, fpu-overflow from fixes branch
  2147. Revision 1.58 2001/09/05 15:19:43 jonas
  2148. * the result of high/low nodes wasn't always resulttypepassed
  2149. Revision 1.57 2001/09/04 14:32:45 jonas
  2150. * simplified det_resulttype code for include/exclude
  2151. * include/exclude doesn't use any helpers anymore in the i386 secondpass
  2152. Revision 1.56 2001/09/04 11:38:55 jonas
  2153. + searchsystype() and searchsystype() functions in symtable
  2154. * changed ninl and nadd to use these functions
  2155. * i386 set comparison functions now return their results in al instead
  2156. of in the flags so that they can be sued as compilerprocs
  2157. - removed all processor specific code from n386add.pas that has to do
  2158. with set handling, it's now all done in nadd.pas
  2159. * fixed fpc_set_contains_sets in genset.inc
  2160. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  2161. helper anymore
  2162. * some small fixes in compproc.inc/set.inc regarding the declaration of
  2163. internal helper types (fpc_small_set and fpc_normal_set)
  2164. Revision 1.55 2001/09/02 21:12:07 peter
  2165. * move class of definitions into type section for delphi
  2166. Revision 1.54 2001/08/28 13:24:46 jonas
  2167. + compilerproc implementation of most string-related type conversions
  2168. - removed all code from the compiler which has been replaced by
  2169. compilerproc implementations (using $ifdef hascompilerproc is not
  2170. necessary in the compiler)
  2171. Revision 1.53 2001/08/27 11:04:41 jonas
  2172. * avoid nonsense range error when using cardinal with value
  2173. > high(longint) as code para with val()
  2174. Revision 1.52 2001/08/26 13:36:40 florian
  2175. * some cg reorganisation
  2176. * some PPC updates
  2177. Revision 1.51 2001/08/24 13:47:27 jonas
  2178. * moved "reverseparameters" from ninl.pas to ncal.pas
  2179. + support for non-persistent temps in ttempcreatenode.create, for use
  2180. with typeconversion nodes
  2181. Revision 1.50 2001/08/24 12:33:54 jonas
  2182. * fixed big bug in handle_str that caused it to (almost) always call
  2183. fpc_<stringtype>_longint
  2184. * fixed small bug in handle_read_write that caused wrong warnigns about
  2185. uninitialized vars with read(ln)
  2186. + handle_val (processor independent val() handling)
  2187. Revision 1.49 2001/08/23 14:28:35 jonas
  2188. + tempcreate/ref/delete nodes (allows the use of temps in the
  2189. resulttype and first pass)
  2190. * made handling of read(ln)/write(ln) processor independent
  2191. * moved processor independent handling for str and reset/rewrite-typed
  2192. from firstpass to resulttype pass
  2193. * changed names of helpers in text.inc to be generic for use as
  2194. compilerprocs + added "iocheck" directive for most of them
  2195. * reading of ordinals is done by procedures instead of functions
  2196. because otherwise FPC_IOCHECK overwrote the result before it could
  2197. be stored elsewhere (range checking still works)
  2198. * compilerprocs can now be used in the system unit before they are
  2199. implemented
  2200. * added note to errore.msg that booleans can't be read using read/readln
  2201. Revision 1.48 2001/08/13 15:39:52 jonas
  2202. * made in_reset_typedfile/in_rewrite_typedfile handling processor
  2203. independent
  2204. Revision 1.47 2001/08/13 12:41:57 jonas
  2205. * made code for str(x,y) completely processor independent
  2206. Revision 1.46 2001/08/06 12:47:31 jonas
  2207. * parameters to FPC_TYPED_WRITE can't be regvars (merged)
  2208. Revision 1.45 2001/08/06 09:44:10 jonas
  2209. + support for high(dynarray) using compilerproc (forgot to commit
  2210. previously)
  2211. Revision 1.44 2001/07/09 21:15:40 peter
  2212. * Length made internal
  2213. * Add array support for Length
  2214. Revision 1.43 2001/07/08 21:00:15 peter
  2215. * various widestring updates, it works now mostly without charset
  2216. mapping supported
  2217. Revision 1.42 2001/06/04 11:48:01 peter
  2218. * better const to var checking
  2219. Revision 1.41 2001/06/03 20:12:53 peter
  2220. * changed int64($ffffffff) that is buggy under 1.0.x to expression
  2221. with a shl
  2222. Revision 1.40 2001/05/06 17:16:43 jonas
  2223. + added warning about missing implementation for high(dynamic_array)
  2224. Revision 1.39 2001/04/26 21:57:05 peter
  2225. * moved code from firstpass to det_resulttype and remove extraneous
  2226. calls to firstcallparan for in_str,in_write,in_val
  2227. Revision 1.38 2001/04/21 12:03:11 peter
  2228. * m68k updates merged from fixes branch
  2229. Revision 1.37 2001/04/13 22:22:30 peter
  2230. * call set_varstate for setlength
  2231. * ptr returns pointerconstnode instead of ordconstnode
  2232. Revision 1.36 2001/04/13 01:22:09 peter
  2233. * symtable change to classes
  2234. * range check generation and errors fixed, make cycle DEBUG=1 works
  2235. * memory leaks fixed
  2236. Revision 1.35 2001/04/05 21:02:13 peter
  2237. * fixed fpu inline functions typeconvs
  2238. Revision 1.34 2001/04/04 22:42:40 peter
  2239. * move constant folding into det_resulttype
  2240. Revision 1.33 2001/04/04 21:30:43 florian
  2241. * applied several fixes to get the DD8 Delphi Unit compiled
  2242. e.g. "forward"-interfaces are working now
  2243. Revision 1.32 2001/04/02 21:20:31 peter
  2244. * resulttype rewrite
  2245. Revision 1.31 2001/03/23 00:16:07 florian
  2246. + some stuff to compile FreeCLX added
  2247. Revision 1.30 2001/03/12 12:47:46 michael
  2248. + Patches from peter
  2249. Revision 1.29 2001/03/03 12:38:08 jonas
  2250. * fixed low() for signed types < 64bit
  2251. Revision 1.28 2001/02/26 19:44:53 peter
  2252. * merged generic m68k updates from fixes branch
  2253. Revision 1.27 2001/02/22 11:24:40 jonas
  2254. * fixed bug in previous fix (hopped over revision 1.26 because that one
  2255. also removed the fix for high(cardinal))
  2256. Revision 1.26 2001/02/21 20:50:59 peter
  2257. * fix to compile again, but high(cardinal) with $R+ still fails!
  2258. Revision 1.25 2001/02/21 12:57:46 jonas
  2259. * fixed high/low for cardinal, int64 and qword
  2260. Revision 1.24 2001/01/06 19:54:11 peter
  2261. * merged fix for 1310
  2262. Revision 1.23 2001/01/06 18:28:39 peter
  2263. * fixed wrong notes about locals
  2264. Revision 1.22 2000/12/31 11:14:10 jonas
  2265. + implemented/fixed docompare() mathods for all nodes (not tested)
  2266. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  2267. and constant strings/chars together
  2268. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  2269. when adding
  2270. Revision 1.21 2000/12/25 00:07:26 peter
  2271. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2272. tlinkedlist objects)
  2273. Revision 1.20 2000/12/17 14:35:41 peter
  2274. * fixed crash with val()
  2275. Revision 1.19 2000/11/29 00:30:33 florian
  2276. * unused units removed from uses clause
  2277. * some changes for widestrings
  2278. Revision 1.18 2000/11/12 15:27:22 jonas
  2279. * also don't do conversion for chars/booleans (hopefully final change :/)
  2280. Revision 1.17 2000/11/11 21:08:13 jonas
  2281. * don't do inc/dec to add/sub conversion for enums
  2282. Revision 1.16 2000/11/11 16:18:35 peter
  2283. * ptr returns farpointer
  2284. Revision 1.15 2000/11/11 15:59:07 jonas
  2285. * convert inc/dec to add/sub when range/overflow checking is on
  2286. Revision 1.14 2000/11/09 17:46:54 florian
  2287. * System.TypeInfo fixed
  2288. + System.Finalize implemented
  2289. + some new keywords for interface support added
  2290. Revision 1.13 2000/11/04 16:48:32 florian
  2291. * innr.inc renamed to make compiler compilation easier because the rtl contains
  2292. a file of the same name
  2293. Revision 1.12 2000/10/31 22:02:48 peter
  2294. * symtable splitted, no real code changes
  2295. Revision 1.11 2000/10/26 14:15:06 jonas
  2296. * fixed setlength for shortstrings
  2297. Revision 1.10 2000/10/21 18:16:11 florian
  2298. * a lot of changes:
  2299. - basic dyn. array support
  2300. - basic C++ support
  2301. - some work for interfaces done
  2302. ....
  2303. Revision 1.9 2000/10/15 08:38:46 jonas
  2304. * added missing getcopy for previous addition
  2305. Revision 1.8 2000/10/14 18:27:53 jonas
  2306. * merged fix for inc/dec on 64bit types from tcinl
  2307. Revision 1.7 2000/10/14 10:14:50 peter
  2308. * moehrendorf oct 2000 rewrite
  2309. Revision 1.6 2000/10/01 19:48:24 peter
  2310. * lot of compile updates for cg11
  2311. Revision 1.5 2000/09/28 19:49:52 florian
  2312. *** empty log message ***
  2313. Revision 1.4 2000/09/28 16:34:47 florian
  2314. *** empty log message ***
  2315. Revision 1.3 2000/09/27 21:33:22 florian
  2316. * finally nadd.pas compiles
  2317. Revision 1.2 2000/09/27 20:25:44 florian
  2318. * more stuff fixed
  2319. Revision 1.1 2000/09/26 14:59:34 florian
  2320. * more conversion work done
  2321. }