ninl.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  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_tp7 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. { assigned(nil) is always false }
  1391. if (tcallparanode(left).left.nodetype=niln) then
  1392. begin
  1393. hp:=cordconstnode.create(0,booltype);
  1394. result:=hp;
  1395. goto myexit;
  1396. end;
  1397. { assigned(pointer(n)) is only false when n=0 }
  1398. if (tcallparanode(left).left.nodetype=pointerconstn) then
  1399. begin
  1400. if tpointerconstnode(tcallparanode(left).left).value=0 then
  1401. hp:=cordconstnode.create(0,booltype)
  1402. else
  1403. hp:=cordconstnode.create(1,booltype);
  1404. result:=hp;
  1405. goto myexit;
  1406. end;
  1407. set_varstate(left,true);
  1408. resulttype:=booltype;
  1409. end;
  1410. in_ofs_x :
  1411. internalerror(2000101001);
  1412. in_seg_x :
  1413. begin
  1414. set_varstate(left,false);
  1415. hp:=cordconstnode.create(0,s32bittype);
  1416. result:=hp;
  1417. goto myexit;
  1418. end;
  1419. in_pred_x,
  1420. in_succ_x:
  1421. begin
  1422. set_varstate(left,true);
  1423. resulttype:=left.resulttype;
  1424. if not is_ordinal(resulttype.def) then
  1425. CGMessage(type_e_ordinal_expr_expected)
  1426. else
  1427. begin
  1428. if (resulttype.def.deftype=enumdef) and
  1429. (tenumdef(resulttype.def).has_jumps) then
  1430. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1431. end;
  1432. { do constant folding after check for jumps }
  1433. if left.nodetype=ordconstn then
  1434. begin
  1435. if inlinenumber=in_succ_x then
  1436. hp:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype)
  1437. else
  1438. hp:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype);
  1439. result:=hp;
  1440. end;
  1441. end;
  1442. in_setlength_x:
  1443. begin
  1444. resulttype:=voidtype;
  1445. if assigned(left) then
  1446. begin
  1447. ppn:=tcallparanode(left);
  1448. counter:=0;
  1449. { check type }
  1450. while assigned(ppn.right) do
  1451. begin
  1452. set_varstate(ppn.left,true);
  1453. inserttypeconv(ppn.left,s32bittype);
  1454. inc(counter);
  1455. ppn:=tcallparanode(ppn.right);
  1456. end;
  1457. { last param must be var }
  1458. valid_for_var(ppn.left);
  1459. set_varstate(ppn.left,false);
  1460. { first param must be a string or dynamic array ...}
  1461. if not((ppn.left.resulttype.def.deftype=stringdef) or
  1462. (is_dynamic_array(ppn.left.resulttype.def))) then
  1463. CGMessage(type_e_mismatch);
  1464. { only dynamic arrays accept more dimensions }
  1465. if (counter>1) then
  1466. if (not(is_dynamic_array(ppn.left.resulttype.def))) then
  1467. CGMessage(type_e_mismatch)
  1468. else
  1469. { check if the amount of dimensions is valid }
  1470. begin
  1471. def := tarraydef(ppn.left.resulttype.def).elementtype.def;
  1472. while counter > 1 do
  1473. begin
  1474. if not(is_dynamic_array(def)) then
  1475. begin
  1476. CGMessage(parser_e_wrong_parameter_size);
  1477. break;
  1478. end;
  1479. dec(counter);
  1480. def := tarraydef(def).elementtype.def;
  1481. end;
  1482. end;
  1483. { convert shortstrings to openstring parameters }
  1484. { (generate the hightree) (JM) }
  1485. if (ppn.left.resulttype.def.deftype = stringdef) and
  1486. (tstringdef(ppn.left.resulttype.def).string_typ =
  1487. st_shortstring) then
  1488. begin
  1489. dummycoll:=tparaitem.create;
  1490. dummycoll.paratyp:=vs_var;
  1491. dummycoll.paratype:=openshortstringtype;
  1492. tcallparanode(ppn).insert_typeconv(dummycoll,false);
  1493. dummycoll.destroy;
  1494. end;
  1495. end
  1496. else
  1497. CGMessage(type_e_mismatch);
  1498. end;
  1499. in_finalize_x:
  1500. begin
  1501. resulttype:=voidtype;
  1502. if assigned(left) and assigned(tcallparanode(left).left) then
  1503. begin
  1504. { first param must be var }
  1505. valid_for_var(tcallparanode(left).left);
  1506. set_varstate(tcallparanode(left).left,true);
  1507. { two parameters?, the last parameter must be a longint }
  1508. if assigned(tcallparanode(left).right) then
  1509. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  1510. end
  1511. else
  1512. CGMessage(type_e_mismatch);
  1513. end;
  1514. in_inc_x,
  1515. in_dec_x:
  1516. begin
  1517. resulttype:=voidtype;
  1518. if assigned(left) then
  1519. begin
  1520. set_varstate(left,true);
  1521. if codegenerror then
  1522. exit;
  1523. { first param must be var }
  1524. valid_for_var(tcallparanode(left).left);
  1525. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1526. is_ordinal(left.resulttype.def) then
  1527. begin
  1528. { value of left gets changed -> must be unique }
  1529. { (bug 1735) (JM) }
  1530. set_unique(tcallparanode(left).left);
  1531. { two paras ? }
  1532. if assigned(tcallparanode(left).right) then
  1533. begin
  1534. { insert a type conversion }
  1535. { the second param is always longint }
  1536. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  1537. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1538. CGMessage(cg_e_illegal_expression);
  1539. end;
  1540. end
  1541. else
  1542. CGMessage(type_e_ordinal_expr_expected);
  1543. end
  1544. else
  1545. CGMessage(type_e_mismatch);
  1546. end;
  1547. in_read_x,
  1548. in_readln_x,
  1549. in_write_x,
  1550. in_writeln_x :
  1551. begin
  1552. result := handle_read_write;
  1553. end;
  1554. in_settextbuf_file_x :
  1555. begin
  1556. resulttype:=voidtype;
  1557. { now we know the type of buffer }
  1558. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1559. hp:=ccallparanode.create(cordconstnode.create(tcallparanode(left).left.resulttype.def.size,s32bittype),left);
  1560. hp:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil);
  1561. left:=nil;
  1562. result:=hp;
  1563. end;
  1564. { the firstpass of the arg has been done in firstcalln ? }
  1565. in_reset_typedfile,
  1566. in_rewrite_typedfile :
  1567. begin
  1568. result := handle_reset_rewrite_typed;
  1569. end;
  1570. in_str_x_string :
  1571. begin
  1572. result := handle_str;
  1573. end;
  1574. in_val_x :
  1575. begin
  1576. result := handle_val;
  1577. end;
  1578. in_include_x_y,
  1579. in_exclude_x_y:
  1580. begin
  1581. resulttype:=voidtype;
  1582. { the parser already checks whether we have two (and exectly two) }
  1583. { parameters (JM) }
  1584. set_varstate(left,true);
  1585. { first param must be var }
  1586. valid_for_var(tcallparanode(left).left);
  1587. { check type }
  1588. if (left.resulttype.def.deftype=setdef) then
  1589. begin
  1590. { insert a type conversion }
  1591. { to the type of the set elements }
  1592. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1593. tsetdef(left.resulttype.def).elementtype);
  1594. end
  1595. else
  1596. CGMessage(type_e_mismatch);
  1597. end;
  1598. in_low_x,
  1599. in_high_x:
  1600. begin
  1601. set_varstate(left,false);
  1602. case left.resulttype.def.deftype of
  1603. orddef,
  1604. enumdef:
  1605. begin
  1606. result:=do_lowhigh(left.resulttype);
  1607. end;
  1608. setdef:
  1609. begin
  1610. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1611. end;
  1612. arraydef:
  1613. begin
  1614. if inlinenumber=in_low_x then
  1615. begin
  1616. result:=cordconstnode.create(tarraydef(left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype);
  1617. end
  1618. else
  1619. begin
  1620. if is_open_array(left.resulttype.def) or
  1621. is_array_of_const(left.resulttype.def) then
  1622. begin
  1623. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1624. result:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1625. end
  1626. else
  1627. if is_dynamic_array(left.resulttype.def) then
  1628. begin
  1629. { can't use inserttypeconv because we need }
  1630. { an explicit type conversion (JM) }
  1631. hp := ctypeconvnode.create(left,voidpointertype);
  1632. hp.toggleflag(nf_explizit);
  1633. hp := ccallparanode.create(hp,nil);
  1634. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1635. { make sure the left node doesn't get disposed, since it's }
  1636. { reused in the new node (JM) }
  1637. left:=nil;
  1638. end
  1639. else
  1640. begin
  1641. result:=cordconstnode.create(tarraydef(left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype);
  1642. end;
  1643. end;
  1644. resulttypepass(result);
  1645. end;
  1646. stringdef:
  1647. begin
  1648. if inlinenumber=in_low_x then
  1649. begin
  1650. hp:=cordconstnode.create(0,u8bittype);
  1651. resulttypepass(hp);
  1652. result:=hp;
  1653. end
  1654. else
  1655. begin
  1656. if is_open_string(left.resulttype.def) then
  1657. begin
  1658. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1659. hp:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1660. resulttypepass(hp);
  1661. result:=hp;
  1662. end
  1663. else
  1664. begin
  1665. hp:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8bittype);
  1666. resulttypepass(hp);
  1667. result:=hp;
  1668. end;
  1669. end;
  1670. end;
  1671. else
  1672. CGMessage(type_e_mismatch);
  1673. end;
  1674. end;
  1675. in_pi:
  1676. begin
  1677. if block_type=bt_const then
  1678. setconstrealvalue(pi)
  1679. else
  1680. resulttype:=s80floattype;
  1681. end;
  1682. in_cos_extended :
  1683. begin
  1684. if left.nodetype in [ordconstn,realconstn] then
  1685. setconstrealvalue(cos(getconstrealvalue))
  1686. else
  1687. begin
  1688. set_varstate(left,true);
  1689. inserttypeconv(left,s80floattype);
  1690. resulttype:=s80floattype;
  1691. end;
  1692. end;
  1693. in_sin_extended :
  1694. begin
  1695. if left.nodetype in [ordconstn,realconstn] then
  1696. setconstrealvalue(sin(getconstrealvalue))
  1697. else
  1698. begin
  1699. set_varstate(left,true);
  1700. inserttypeconv(left,s80floattype);
  1701. resulttype:=s80floattype;
  1702. end;
  1703. end;
  1704. in_arctan_extended :
  1705. begin
  1706. if left.nodetype in [ordconstn,realconstn] then
  1707. setconstrealvalue(arctan(getconstrealvalue))
  1708. else
  1709. begin
  1710. set_varstate(left,true);
  1711. inserttypeconv(left,s80floattype);
  1712. resulttype:=s80floattype;
  1713. end;
  1714. end;
  1715. in_abs_extended :
  1716. begin
  1717. if left.nodetype in [ordconstn,realconstn] then
  1718. setconstrealvalue(abs(getconstrealvalue))
  1719. else
  1720. begin
  1721. set_varstate(left,true);
  1722. inserttypeconv(left,s80floattype);
  1723. resulttype:=s80floattype;
  1724. end;
  1725. end;
  1726. in_sqr_extended :
  1727. begin
  1728. if left.nodetype in [ordconstn,realconstn] then
  1729. setconstrealvalue(sqr(getconstrealvalue))
  1730. else
  1731. begin
  1732. set_varstate(left,true);
  1733. inserttypeconv(left,s80floattype);
  1734. resulttype:=s80floattype;
  1735. end;
  1736. end;
  1737. in_sqrt_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(sqrt(vr));
  1749. end
  1750. else
  1751. begin
  1752. set_varstate(left,true);
  1753. inserttypeconv(left,s80floattype);
  1754. resulttype:=s80floattype;
  1755. end;
  1756. end;
  1757. in_ln_extended :
  1758. begin
  1759. if left.nodetype in [ordconstn,realconstn] then
  1760. begin
  1761. vr:=getconstrealvalue;
  1762. if vr<=0.0 then
  1763. begin
  1764. CGMessage(type_e_wrong_math_argument);
  1765. setconstrealvalue(0);
  1766. end
  1767. else
  1768. setconstrealvalue(ln(vr));
  1769. end
  1770. else
  1771. begin
  1772. set_varstate(left,true);
  1773. inserttypeconv(left,s80floattype);
  1774. resulttype:=s80floattype;
  1775. end;
  1776. end;
  1777. {$ifdef SUPPORT_MMX}
  1778. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1779. begin
  1780. end;
  1781. {$endif SUPPORT_MMX}
  1782. in_assert_x_y :
  1783. begin
  1784. resulttype:=voidtype;
  1785. if assigned(left) then
  1786. begin
  1787. set_varstate(left,true);
  1788. { check type }
  1789. if is_boolean(left.resulttype.def) then
  1790. begin
  1791. { must always be a string }
  1792. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1793. end
  1794. else
  1795. CGMessage(type_e_mismatch);
  1796. end
  1797. else
  1798. CGMessage(type_e_mismatch);
  1799. end;
  1800. else
  1801. internalerror(8);
  1802. end;
  1803. end;
  1804. myexit:
  1805. { Run get_paratype again to update maybe inserted typeconvs }
  1806. if not codegenerror then
  1807. begin
  1808. if assigned(left) and
  1809. (left.nodetype=callparan) then
  1810. tcallparanode(left).get_paratype;
  1811. end;
  1812. dec(parsing_para_level);
  1813. end;
  1814. {$ifdef fpc}
  1815. {$maxfpuregisters 0}
  1816. {$endif fpc}
  1817. function tinlinenode.pass_1 : tnode;
  1818. var
  1819. srsym : tsym;
  1820. hp,hpp : tnode;
  1821. shiftconst: longint;
  1822. begin
  1823. result:=nil;
  1824. { if we handle writeln; left contains no valid address }
  1825. if assigned(left) then
  1826. begin
  1827. if left.nodetype=callparan then
  1828. tcallparanode(left).firstcallparan(nil,false)
  1829. else
  1830. firstpass(left);
  1831. left_max;
  1832. set_location(location,left.location);
  1833. end;
  1834. inc(parsing_para_level);
  1835. { intern const should already be handled }
  1836. if nf_inlineconst in flags then
  1837. internalerror(200104044);
  1838. case inlinenumber of
  1839. in_lo_qword,
  1840. in_hi_qword,
  1841. in_lo_long,
  1842. in_hi_long,
  1843. in_lo_word,
  1844. in_hi_word:
  1845. begin
  1846. shiftconst := 0;
  1847. case inlinenumber of
  1848. in_hi_qword:
  1849. shiftconst := 32;
  1850. in_hi_long:
  1851. shiftconst := 16;
  1852. in_hi_word:
  1853. shiftconst := 8;
  1854. end;
  1855. if shiftconst <> 0 then
  1856. result := ctypeconvnode.create(cshlshrnode.create(shrn,left,
  1857. cordconstnode.create(shiftconst,u32bittype)),resulttype)
  1858. else
  1859. result := ctypeconvnode.create(left,resulttype);
  1860. left := nil;
  1861. include(result.flags,nf_explizit);
  1862. firstpass(result);
  1863. end;
  1864. in_sizeof_x:
  1865. begin
  1866. if push_high_param(left.resulttype.def) then
  1867. begin
  1868. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1869. hp:=caddnode.create(addn,cloadnode.create(tvarsym(srsym),tloadnode(left).symtable),
  1870. cordconstnode.create(1,s32bittype));
  1871. if (left.resulttype.def.deftype=arraydef) and
  1872. (tarraydef(left.resulttype.def).elesize<>1) then
  1873. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(left.resulttype.def).elesize,s32bittype));
  1874. firstpass(hp);
  1875. result:=hp;
  1876. end
  1877. else
  1878. begin
  1879. if registers32<1 then
  1880. registers32:=1;
  1881. location.loc:=LOC_REGISTER;
  1882. end;
  1883. end;
  1884. in_typeof_x:
  1885. begin
  1886. if registers32<1 then
  1887. registers32:=1;
  1888. location.loc:=LOC_REGISTER;
  1889. end;
  1890. in_ord_x,
  1891. in_chr_byte:
  1892. begin
  1893. { should not happend as it's converted to typeconv }
  1894. internalerror(200104045);
  1895. end;
  1896. in_length_x:
  1897. begin
  1898. if is_shortstring(left.resulttype.def) then
  1899. location.loc:=LOC_REFERENCE
  1900. else
  1901. begin
  1902. { ansi/wide string }
  1903. if registers32<1 then
  1904. registers32:=1;
  1905. location.loc:=LOC_REGISTER;
  1906. end;
  1907. end;
  1908. in_typeinfo_x:
  1909. begin
  1910. location.loc:=LOC_REGISTER;
  1911. registers32:=1;
  1912. end;
  1913. in_assigned_x:
  1914. begin
  1915. location.loc:=LOC_FLAGS;
  1916. end;
  1917. in_ofs_x :
  1918. internalerror(2000101001);
  1919. in_seg_x :
  1920. internalerror(200104046);
  1921. in_pred_x,
  1922. in_succ_x:
  1923. begin
  1924. if is_64bitint(resulttype.def) then
  1925. begin
  1926. if (registers32<2) then
  1927. registers32:=2
  1928. end
  1929. else
  1930. begin
  1931. if (registers32<1) then
  1932. registers32:=1;
  1933. end;
  1934. location.loc:=LOC_REGISTER;
  1935. end;
  1936. in_setlength_x:
  1937. begin
  1938. end;
  1939. in_finalize_x:
  1940. begin
  1941. end;
  1942. in_inc_x,
  1943. in_dec_x:
  1944. begin
  1945. { check type }
  1946. if is_64bitint(left.resulttype.def) or
  1947. { range/overflow checking doesn't work properly }
  1948. { with the inc/dec code that's generated (JM) }
  1949. ((left.resulttype.def.deftype = orddef) and
  1950. not(is_char(left.resulttype.def)) and
  1951. not(is_boolean(left.resulttype.def)) and
  1952. (aktlocalswitches *
  1953. [cs_check_overflow,cs_check_range] <> [])) then
  1954. { convert to simple add (JM) }
  1955. begin
  1956. { extra parameter? }
  1957. if assigned(tcallparanode(left).right) then
  1958. begin
  1959. { Yes, use for add node }
  1960. hpp := tcallparanode(tcallparanode(left).right).left;
  1961. tcallparanode(tcallparanode(left).right).left := nil;
  1962. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1963. CGMessage(cg_e_illegal_expression);
  1964. end
  1965. else
  1966. { no, create constant 1 }
  1967. hpp := cordconstnode.create(1,s32bittype);
  1968. { addition/substraction depending on inc/dec }
  1969. if inlinenumber = in_inc_x then
  1970. hp := caddnode.create(addn,tcallparanode(left).left.getcopy,hpp)
  1971. else
  1972. hp := caddnode.create(subn,tcallparanode(left).left.getcopy,hpp);
  1973. { assign result of addition }
  1974. hpp := cassignmentnode.create(tcallparanode(left).left,hp);
  1975. tcallparanode(left).left := nil;
  1976. { firstpass it }
  1977. firstpass(hpp);
  1978. { return new node }
  1979. result := hpp;
  1980. end
  1981. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1982. is_ordinal(left.resulttype.def) then
  1983. begin
  1984. { two paras ? }
  1985. if assigned(tcallparanode(left).right) then
  1986. begin
  1987. { need we an additional register ? }
  1988. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  1989. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  1990. (tcallparanode(tcallparanode(left).right).left.registers32<=1) then
  1991. inc(registers32);
  1992. { do we need an additional register to restore the first parameter? }
  1993. if tcallparanode(tcallparanode(left).right).left.registers32>=registers32 then
  1994. inc(registers32);
  1995. end;
  1996. end;
  1997. end;
  1998. in_read_x,
  1999. in_readln_x,
  2000. in_write_x,
  2001. in_writeln_x :
  2002. begin
  2003. { should be handled by det_resulttype }
  2004. internalerror(200108234);
  2005. end;
  2006. in_settextbuf_file_x :
  2007. internalerror(200104262);
  2008. in_reset_typedfile,
  2009. in_rewrite_typedfile :
  2010. begin
  2011. { should already be removed in det_resulttype (JM) }
  2012. internalerror(200108236);
  2013. end;
  2014. in_str_x_string :
  2015. begin
  2016. { should already be removed in det_resulttype (JM) }
  2017. internalerror(200108235);
  2018. end;
  2019. in_val_x :
  2020. begin
  2021. { should already be removed in det_resulttype (JM) }
  2022. internalerror(200108242);
  2023. end;
  2024. in_include_x_y,
  2025. in_exclude_x_y:
  2026. begin
  2027. registers32:=left.registers32;
  2028. registersfpu:=left.registersfpu;
  2029. {$ifdef SUPPORT_MMX}
  2030. registersmmx:=left.registersmmx;
  2031. {$endif SUPPORT_MMX}
  2032. end;
  2033. in_low_x,
  2034. in_high_x:
  2035. internalerror(200104047);
  2036. in_cos_extended:
  2037. begin
  2038. location.loc:=LOC_FPU;
  2039. registers32:=left.registers32;
  2040. registersfpu:=max(left.registersfpu,1);
  2041. {$ifdef SUPPORT_MMX}
  2042. registersmmx:=left.registersmmx;
  2043. {$endif SUPPORT_MMX}
  2044. end;
  2045. in_sin_extended:
  2046. begin
  2047. location.loc:=LOC_FPU;
  2048. registers32:=left.registers32;
  2049. registersfpu:=max(left.registersfpu,1);
  2050. {$ifdef SUPPORT_MMX}
  2051. registersmmx:=left.registersmmx;
  2052. {$endif SUPPORT_MMX}
  2053. end;
  2054. in_arctan_extended:
  2055. begin
  2056. location.loc:=LOC_FPU;
  2057. registers32:=left.registers32;
  2058. registersfpu:=max(left.registersfpu,2);
  2059. {$ifdef SUPPORT_MMX}
  2060. registersmmx:=left.registersmmx;
  2061. {$endif SUPPORT_MMX}
  2062. end;
  2063. in_pi:
  2064. begin
  2065. location.loc:=LOC_FPU;
  2066. registersfpu:=1;
  2067. end;
  2068. in_abs_extended:
  2069. begin
  2070. location.loc:=LOC_FPU;
  2071. registers32:=left.registers32;
  2072. registersfpu:=max(left.registersfpu,1);
  2073. {$ifdef SUPPORT_MMX}
  2074. registersmmx:=left.registersmmx;
  2075. {$endif SUPPORT_MMX}
  2076. end;
  2077. in_sqr_extended:
  2078. begin
  2079. location.loc:=LOC_FPU;
  2080. registers32:=left.registers32;
  2081. registersfpu:=max(left.registersfpu,1);
  2082. {$ifdef SUPPORT_MMX}
  2083. registersmmx:=left.registersmmx;
  2084. {$endif SUPPORT_MMX}
  2085. end;
  2086. in_sqrt_extended:
  2087. begin
  2088. location.loc:=LOC_FPU;
  2089. registers32:=left.registers32;
  2090. registersfpu:=max(left.registersfpu,1);
  2091. {$ifdef SUPPORT_MMX}
  2092. registersmmx:=left.registersmmx;
  2093. {$endif SUPPORT_MMX}
  2094. end;
  2095. in_ln_extended:
  2096. begin
  2097. location.loc:=LOC_FPU;
  2098. registers32:=left.registers32;
  2099. registersfpu:=max(left.registersfpu,2);
  2100. {$ifdef SUPPORT_MMX}
  2101. registersmmx:=left.registersmmx;
  2102. {$endif SUPPORT_MMX}
  2103. end;
  2104. {$ifdef SUPPORT_MMX}
  2105. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2106. begin
  2107. end;
  2108. {$endif SUPPORT_MMX}
  2109. in_assert_x_y :
  2110. begin
  2111. { We've checked the whole statement for correctness, now we
  2112. can remove it if assertions are off }
  2113. if not(cs_do_assertion in aktlocalswitches) then
  2114. begin
  2115. { we need a valid node, so insert a nothingn }
  2116. result:=cnothingnode.create;
  2117. end
  2118. else
  2119. begin
  2120. registers32:=left.registers32;
  2121. registersfpu:=left.registersfpu;
  2122. {$ifdef SUPPORT_MMX}
  2123. registersmmx:=left.registersmmx;
  2124. {$endif SUPPORT_MMX}
  2125. end;
  2126. end;
  2127. else
  2128. internalerror(8);
  2129. end;
  2130. dec(parsing_para_level);
  2131. end;
  2132. {$ifdef fpc}
  2133. {$maxfpuregisters default}
  2134. {$endif fpc}
  2135. function tinlinenode.docompare(p: tnode): boolean;
  2136. begin
  2137. docompare :=
  2138. inherited docompare(p) and
  2139. (inlinenumber = tinlinenode(p).inlinenumber);
  2140. end;
  2141. begin
  2142. cinlinenode:=tinlinenode;
  2143. end.
  2144. {
  2145. $Log$
  2146. Revision 1.69 2002-01-24 18:25:48 peter
  2147. * implicit result variable generation for assembler routines
  2148. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2149. Revision 1.68 2002/01/19 11:53:56 peter
  2150. * constant evaluation for assinged added
  2151. Revision 1.67 2001/12/28 14:09:21 jonas
  2152. * fixed web bug 1735 (argument of inc/dec must be made unique) ("merged")
  2153. Revision 1.66 2001/12/10 14:26:22 jonas
  2154. - removed unnecessary resulttypepass call
  2155. Revision 1.65 2001/12/04 15:59:03 jonas
  2156. * converted lo/hi to processor independent code, generated code is the
  2157. same as before (when turning on the optimizer)
  2158. Revision 1.64 2001/12/03 14:21:34 jonas
  2159. * fixed web bug 1693 (dynarray support for length)
  2160. Revision 1.63 2001/10/24 16:17:36 jonas
  2161. * fixed web bug 1621 (write(typed_file,function_call) works again)
  2162. * allow write(typed_file,procvar_call) too (it was already allowed for
  2163. text file writes)
  2164. Revision 1.62 2001/09/30 16:16:28 jonas
  2165. - removed unused units form uses-clause and unused local vars
  2166. Revision 1.61 2001/09/24 16:09:55 jonas
  2167. * check if amount of dimensions passed to setlength for dynamic arrays
  2168. is correct
  2169. Revision 1.60 2001/09/24 11:35:55 jonas
  2170. * fix from Pavel V. Ozersk to accept multiple dimensions for setlength
  2171. and dynamical arrays
  2172. Revision 1.59 2001/09/17 21:29:12 peter
  2173. * merged netbsd, fpu-overflow from fixes branch
  2174. Revision 1.58 2001/09/05 15:19:43 jonas
  2175. * the result of high/low nodes wasn't always resulttypepassed
  2176. Revision 1.57 2001/09/04 14:32:45 jonas
  2177. * simplified det_resulttype code for include/exclude
  2178. * include/exclude doesn't use any helpers anymore in the i386 secondpass
  2179. Revision 1.56 2001/09/04 11:38:55 jonas
  2180. + searchsystype() and searchsystype() functions in symtable
  2181. * changed ninl and nadd to use these functions
  2182. * i386 set comparison functions now return their results in al instead
  2183. of in the flags so that they can be sued as compilerprocs
  2184. - removed all processor specific code from n386add.pas that has to do
  2185. with set handling, it's now all done in nadd.pas
  2186. * fixed fpc_set_contains_sets in genset.inc
  2187. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  2188. helper anymore
  2189. * some small fixes in compproc.inc/set.inc regarding the declaration of
  2190. internal helper types (fpc_small_set and fpc_normal_set)
  2191. Revision 1.55 2001/09/02 21:12:07 peter
  2192. * move class of definitions into type section for delphi
  2193. Revision 1.54 2001/08/28 13:24:46 jonas
  2194. + compilerproc implementation of most string-related type conversions
  2195. - removed all code from the compiler which has been replaced by
  2196. compilerproc implementations (using $ifdef hascompilerproc is not
  2197. necessary in the compiler)
  2198. Revision 1.53 2001/08/27 11:04:41 jonas
  2199. * avoid nonsense range error when using cardinal with value
  2200. > high(longint) as code para with val()
  2201. Revision 1.52 2001/08/26 13:36:40 florian
  2202. * some cg reorganisation
  2203. * some PPC updates
  2204. Revision 1.51 2001/08/24 13:47:27 jonas
  2205. * moved "reverseparameters" from ninl.pas to ncal.pas
  2206. + support for non-persistent temps in ttempcreatenode.create, for use
  2207. with typeconversion nodes
  2208. Revision 1.50 2001/08/24 12:33:54 jonas
  2209. * fixed big bug in handle_str that caused it to (almost) always call
  2210. fpc_<stringtype>_longint
  2211. * fixed small bug in handle_read_write that caused wrong warnigns about
  2212. uninitialized vars with read(ln)
  2213. + handle_val (processor independent val() handling)
  2214. Revision 1.49 2001/08/23 14:28:35 jonas
  2215. + tempcreate/ref/delete nodes (allows the use of temps in the
  2216. resulttype and first pass)
  2217. * made handling of read(ln)/write(ln) processor independent
  2218. * moved processor independent handling for str and reset/rewrite-typed
  2219. from firstpass to resulttype pass
  2220. * changed names of helpers in text.inc to be generic for use as
  2221. compilerprocs + added "iocheck" directive for most of them
  2222. * reading of ordinals is done by procedures instead of functions
  2223. because otherwise FPC_IOCHECK overwrote the result before it could
  2224. be stored elsewhere (range checking still works)
  2225. * compilerprocs can now be used in the system unit before they are
  2226. implemented
  2227. * added note to errore.msg that booleans can't be read using read/readln
  2228. Revision 1.48 2001/08/13 15:39:52 jonas
  2229. * made in_reset_typedfile/in_rewrite_typedfile handling processor
  2230. independent
  2231. Revision 1.47 2001/08/13 12:41:57 jonas
  2232. * made code for str(x,y) completely processor independent
  2233. Revision 1.46 2001/08/06 12:47:31 jonas
  2234. * parameters to FPC_TYPED_WRITE can't be regvars (merged)
  2235. Revision 1.45 2001/08/06 09:44:10 jonas
  2236. + support for high(dynarray) using compilerproc (forgot to commit
  2237. previously)
  2238. Revision 1.44 2001/07/09 21:15:40 peter
  2239. * Length made internal
  2240. * Add array support for Length
  2241. Revision 1.43 2001/07/08 21:00:15 peter
  2242. * various widestring updates, it works now mostly without charset
  2243. mapping supported
  2244. Revision 1.42 2001/06/04 11:48:01 peter
  2245. * better const to var checking
  2246. Revision 1.41 2001/06/03 20:12:53 peter
  2247. * changed int64($ffffffff) that is buggy under 1.0.x to expression
  2248. with a shl
  2249. Revision 1.40 2001/05/06 17:16:43 jonas
  2250. + added warning about missing implementation for high(dynamic_array)
  2251. Revision 1.39 2001/04/26 21:57:05 peter
  2252. * moved code from firstpass to det_resulttype and remove extraneous
  2253. calls to firstcallparan for in_str,in_write,in_val
  2254. Revision 1.38 2001/04/21 12:03:11 peter
  2255. * m68k updates merged from fixes branch
  2256. Revision 1.37 2001/04/13 22:22:30 peter
  2257. * call set_varstate for setlength
  2258. * ptr returns pointerconstnode instead of ordconstnode
  2259. Revision 1.36 2001/04/13 01:22:09 peter
  2260. * symtable change to classes
  2261. * range check generation and errors fixed, make cycle DEBUG=1 works
  2262. * memory leaks fixed
  2263. Revision 1.35 2001/04/05 21:02:13 peter
  2264. * fixed fpu inline functions typeconvs
  2265. Revision 1.34 2001/04/04 22:42:40 peter
  2266. * move constant folding into det_resulttype
  2267. Revision 1.33 2001/04/04 21:30:43 florian
  2268. * applied several fixes to get the DD8 Delphi Unit compiled
  2269. e.g. "forward"-interfaces are working now
  2270. Revision 1.32 2001/04/02 21:20:31 peter
  2271. * resulttype rewrite
  2272. Revision 1.31 2001/03/23 00:16:07 florian
  2273. + some stuff to compile FreeCLX added
  2274. Revision 1.30 2001/03/12 12:47:46 michael
  2275. + Patches from peter
  2276. Revision 1.29 2001/03/03 12:38:08 jonas
  2277. * fixed low() for signed types < 64bit
  2278. Revision 1.28 2001/02/26 19:44:53 peter
  2279. * merged generic m68k updates from fixes branch
  2280. Revision 1.27 2001/02/22 11:24:40 jonas
  2281. * fixed bug in previous fix (hopped over revision 1.26 because that one
  2282. also removed the fix for high(cardinal))
  2283. Revision 1.26 2001/02/21 20:50:59 peter
  2284. * fix to compile again, but high(cardinal) with $R+ still fails!
  2285. Revision 1.25 2001/02/21 12:57:46 jonas
  2286. * fixed high/low for cardinal, int64 and qword
  2287. Revision 1.24 2001/01/06 19:54:11 peter
  2288. * merged fix for 1310
  2289. Revision 1.23 2001/01/06 18:28:39 peter
  2290. * fixed wrong notes about locals
  2291. Revision 1.22 2000/12/31 11:14:10 jonas
  2292. + implemented/fixed docompare() mathods for all nodes (not tested)
  2293. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  2294. and constant strings/chars together
  2295. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  2296. when adding
  2297. Revision 1.21 2000/12/25 00:07:26 peter
  2298. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2299. tlinkedlist objects)
  2300. Revision 1.20 2000/12/17 14:35:41 peter
  2301. * fixed crash with val()
  2302. Revision 1.19 2000/11/29 00:30:33 florian
  2303. * unused units removed from uses clause
  2304. * some changes for widestrings
  2305. Revision 1.18 2000/11/12 15:27:22 jonas
  2306. * also don't do conversion for chars/booleans (hopefully final change :/)
  2307. Revision 1.17 2000/11/11 21:08:13 jonas
  2308. * don't do inc/dec to add/sub conversion for enums
  2309. Revision 1.16 2000/11/11 16:18:35 peter
  2310. * ptr returns farpointer
  2311. Revision 1.15 2000/11/11 15:59:07 jonas
  2312. * convert inc/dec to add/sub when range/overflow checking is on
  2313. Revision 1.14 2000/11/09 17:46:54 florian
  2314. * System.TypeInfo fixed
  2315. + System.Finalize implemented
  2316. + some new keywords for interface support added
  2317. Revision 1.13 2000/11/04 16:48:32 florian
  2318. * innr.inc renamed to make compiler compilation easier because the rtl contains
  2319. a file of the same name
  2320. Revision 1.12 2000/10/31 22:02:48 peter
  2321. * symtable splitted, no real code changes
  2322. Revision 1.11 2000/10/26 14:15:06 jonas
  2323. * fixed setlength for shortstrings
  2324. Revision 1.10 2000/10/21 18:16:11 florian
  2325. * a lot of changes:
  2326. - basic dyn. array support
  2327. - basic C++ support
  2328. - some work for interfaces done
  2329. ....
  2330. Revision 1.9 2000/10/15 08:38:46 jonas
  2331. * added missing getcopy for previous addition
  2332. Revision 1.8 2000/10/14 18:27:53 jonas
  2333. * merged fix for inc/dec on 64bit types from tcinl
  2334. Revision 1.7 2000/10/14 10:14:50 peter
  2335. * moehrendorf oct 2000 rewrite
  2336. Revision 1.6 2000/10/01 19:48:24 peter
  2337. * lot of compile updates for cg11
  2338. Revision 1.5 2000/09/28 19:49:52 florian
  2339. *** empty log message ***
  2340. Revision 1.4 2000/09/28 16:34:47 florian
  2341. *** empty log message ***
  2342. Revision 1.3 2000/09/27 21:33:22 florian
  2343. * finally nadd.pas compiles
  2344. Revision 1.2 2000/09/27 20:25:44 florian
  2345. * more stuff fixed
  2346. Revision 1.1 2000/09/26 14:59:34 florian
  2347. * more conversion work done
  2348. }