ninl.pas 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  28. procedure ppuwrite(ppufile:tcompilerppufile);override;
  29. function dogetcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function docompare(p: tnode): boolean; override;
  33. { pack and unpack are changed into for-loops by the compiler }
  34. function first_pack_unpack: tnode; virtual;
  35. { All the following routines currently
  36. call compilerprocs, unless they are
  37. overriden in which case, the code
  38. generator handles them.
  39. }
  40. function first_pi: tnode ; virtual;
  41. function first_arctan_real: tnode; virtual;
  42. function first_abs_real: tnode; virtual;
  43. function first_sqr_real: tnode; virtual;
  44. function first_sqrt_real: tnode; virtual;
  45. function first_ln_real: tnode; virtual;
  46. function first_cos_real: tnode; virtual;
  47. function first_sin_real: tnode; virtual;
  48. function first_exp_real: tnode; virtual;
  49. function first_frac_real: tnode; virtual;
  50. function first_round_real: tnode; virtual;
  51. function first_trunc_real: tnode; virtual;
  52. function first_int_real: tnode; virtual;
  53. private
  54. function handle_str: tnode;
  55. function handle_reset_rewrite_typed: tnode;
  56. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  57. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  58. function handle_read_write: tnode;
  59. function handle_val: tnode;
  60. end;
  61. tinlinenodeclass = class of tinlinenode;
  62. var
  63. cinlinenode : tinlinenodeclass;
  64. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  65. implementation
  66. uses
  67. verbose,globals,systems,constexp,
  68. globtype, cutils,
  69. symconst,symdef,symsym,symtable,paramgr,defutil,
  70. pass_1,
  71. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  72. cgbase,procinfo
  73. ;
  74. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  75. begin
  76. geninlinenode:=cinlinenode.create(number,is_const,l);
  77. end;
  78. {*****************************************************************************
  79. TINLINENODE
  80. *****************************************************************************}
  81. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  82. begin
  83. inherited create(inlinen,l);
  84. if is_const then
  85. include(flags,nf_inlineconst);
  86. inlinenumber:=number;
  87. end;
  88. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  89. begin
  90. inherited ppuload(t,ppufile);
  91. inlinenumber:=ppufile.getbyte;
  92. end;
  93. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  94. begin
  95. inherited ppuwrite(ppufile);
  96. ppufile.putbyte(inlinenumber);
  97. end;
  98. function tinlinenode.dogetcopy : tnode;
  99. var
  100. n : tinlinenode;
  101. begin
  102. n:=tinlinenode(inherited dogetcopy);
  103. n.inlinenumber:=inlinenumber;
  104. result:=n;
  105. end;
  106. function tinlinenode.handle_str : tnode;
  107. var
  108. lenpara,
  109. fracpara,
  110. newparas,
  111. tmppara,
  112. dest,
  113. source : tcallparanode;
  114. procname: string;
  115. is_real,is_enum : boolean;
  116. rt : aint;
  117. begin
  118. result := cerrornode.create;
  119. { make sure we got at least two parameters (if we got only one, }
  120. { this parameter may not be encapsulated in a callparan) }
  121. if not assigned(left) or
  122. (left.nodetype <> callparan) then
  123. begin
  124. CGMessage1(parser_e_wrong_parameter_size,'Str');
  125. exit;
  126. end;
  127. { get destination string }
  128. dest := tcallparanode(left);
  129. { get source para (number) }
  130. source := dest;
  131. while assigned(source.right) do
  132. source := tcallparanode(source.right);
  133. { destination parameter must be a normal (not a colon) parameter, this
  134. check is needed because str(v:len) also has 2 parameters }
  135. if (source=dest) or
  136. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  137. begin
  138. CGMessage1(parser_e_wrong_parameter_size,'Str');
  139. exit;
  140. end;
  141. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  142. is_enum:=source.left.resultdef.typ=enumdef;
  143. if ((dest.left.resultdef.typ<>stringdef) and
  144. not(is_chararray(dest.left.resultdef))) or
  145. not(is_real or is_enum or
  146. (source.left.resultdef.typ=orddef)) then
  147. begin
  148. CGMessagePos(fileinfo,parser_e_illegal_expression);
  149. exit;
  150. end;
  151. { get len/frac parameters }
  152. lenpara := nil;
  153. fracpara := nil;
  154. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  155. begin
  156. lenpara := tcallparanode(dest.right);
  157. { we can let the callnode do the type checking of these parameters too, }
  158. { but then the error messages aren't as nice }
  159. if not is_integer(lenpara.resultdef) then
  160. begin
  161. CGMessagePos1(lenpara.fileinfo,
  162. type_e_integer_expr_expected,lenpara.resultdef.typename);
  163. exit;
  164. end;
  165. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  166. begin
  167. { parameters are in reverse order! }
  168. fracpara := lenpara;
  169. lenpara := tcallparanode(lenpara.right);
  170. if not is_real then
  171. begin
  172. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  173. exit
  174. end;
  175. if not is_integer(lenpara.resultdef) then
  176. begin
  177. CGMessagePos1(lenpara.fileinfo,
  178. type_e_integer_expr_expected,lenpara.resultdef.typename);
  179. exit;
  180. end;
  181. end;
  182. end;
  183. { generate the parameter list for the compilerproc }
  184. newparas := dest;
  185. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  186. if is_real then
  187. begin
  188. { insert realtype parameter }
  189. if not is_currency(source.resultdef) then
  190. begin
  191. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  192. newparas.right := ccallparanode.create(cordconstnode.create(
  193. rt,s32inttype,true),newparas.right);
  194. tmppara:=tcallparanode(newparas.right);
  195. end
  196. else
  197. tmppara:=newparas;
  198. { if necessary, insert a fraction parameter }
  199. if not assigned(fracpara) then
  200. begin
  201. tmppara.right := ccallparanode.create(
  202. cordconstnode.create(int64(-1),s32inttype,false),
  203. tmppara.right);
  204. fracpara := tcallparanode(tmppara.right);
  205. end;
  206. { if necessary, insert a length para }
  207. if not assigned(lenpara) then
  208. fracpara.right := ccallparanode.create(
  209. cordconstnode.create(int64(-32767),s32inttype,false),
  210. fracpara.right);
  211. end
  212. else if is_enum then
  213. begin
  214. {Insert a reference to the ord2string index.}
  215. newparas.right:=Ccallparanode.create(
  216. Caddrnode.create_internal(
  217. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  218. ),
  219. newparas.right);
  220. {Insert a reference to the typinfo.}
  221. newparas.right:=Ccallparanode.create(
  222. Caddrnode.create_internal(
  223. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  224. ),
  225. newparas.right);
  226. {Insert a type conversion from the enumeration to longint.}
  227. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  228. typecheckpass(source.left);
  229. { if necessary, insert a length para }
  230. if not assigned(lenpara) then
  231. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  232. Ccallparanode.create(
  233. cordconstnode.create(int64(-1),s32inttype,false),
  234. Tcallparanode(Tcallparanode(newparas.right).right).right
  235. );
  236. end
  237. else
  238. { for a normal parameter, insert a only length parameter if one is missing }
  239. if not assigned(lenpara) then
  240. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  241. newparas.right);
  242. { remove the parameters from the original node so they won't get disposed, }
  243. { since they're reused }
  244. left := nil;
  245. { create procedure name }
  246. if is_chararray(dest.resultdef) then
  247. procname:='fpc_chararray_'
  248. else
  249. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  250. if is_real then
  251. if is_currency(source.resultdef) then
  252. procname := procname + 'currency'
  253. else
  254. procname := procname + 'float'
  255. else if is_enum then
  256. procname:=procname+'enum'
  257. else
  258. case torddef(source.resultdef).ordtype of
  259. {$ifdef cpu64bit}
  260. u64bit:
  261. procname := procname + 'uint';
  262. {$else}
  263. u32bit:
  264. procname := procname + 'uint';
  265. u64bit:
  266. procname := procname + 'qword';
  267. scurrency,
  268. s64bit:
  269. procname := procname + 'int64';
  270. {$endif}
  271. else
  272. procname := procname + 'sint';
  273. end;
  274. { free the errornode we generated in the beginning }
  275. result.free;
  276. { create the call node, }
  277. result := ccallnode.createintern(procname,newparas);
  278. end;
  279. function tinlinenode.handle_reset_rewrite_typed: tnode;
  280. begin
  281. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  282. { a typed file as argument and we don't have to check it again (JM) }
  283. { add the recsize parameter }
  284. { note: for some reason, the parameter of intern procedures with only one }
  285. { parameter is gets lifted out of its original tcallparanode (see round }
  286. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  287. left := ccallparanode.create(cordconstnode.create(
  288. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  289. ccallparanode.create(left,nil));
  290. { create the correct call }
  291. if inlinenumber=in_reset_typedfile then
  292. result := ccallnode.createintern('fpc_reset_typed',left)
  293. else
  294. result := ccallnode.createintern('fpc_rewrite_typed',left);
  295. { make sure left doesn't get disposed, since we use it in the new call }
  296. left := nil;
  297. end;
  298. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  299. {Read(ln)/write(ln) for text files.}
  300. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  301. var error_para,is_real,special_handling,found_error,do_read:boolean;
  302. p1:Tnode;
  303. nextpara,
  304. indexpara,
  305. lenpara,
  306. para,
  307. fracpara:Tcallparanode;
  308. temp:Ttempcreatenode;
  309. readfunctype:Tdef;
  310. name:string[31];
  311. begin
  312. para:=Tcallparanode(params);
  313. found_error:=false;
  314. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  315. while assigned(para) do
  316. begin
  317. { is this parameter faulty? }
  318. error_para:=false;
  319. { is this parameter a real? }
  320. is_real:=false;
  321. { type used for the read(), this is used to check
  322. whether a temp is needed for range checking }
  323. readfunctype:=nil;
  324. { can't read/write types }
  325. if para.left.nodetype=typen then
  326. begin
  327. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  328. error_para := true;
  329. end;
  330. { support writeln(procvar) }
  331. if para.left.resultdef.typ=procvardef then
  332. begin
  333. p1:=ccallnode.create_procvar(nil,para.left);
  334. typecheckpass(p1);
  335. para.left:=p1;
  336. end;
  337. case para.left.resultdef.typ of
  338. stringdef :
  339. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  340. pointerdef :
  341. begin
  342. if (not is_pchar(para.left.resultdef)) or do_read then
  343. begin
  344. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  345. error_para := true;
  346. end
  347. else
  348. name:=procprefixes[do_read]+'pchar_as_pointer';
  349. end;
  350. floatdef :
  351. begin
  352. is_real:=true;
  353. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  354. name := procprefixes[do_read]+'currency'
  355. else
  356. begin
  357. name := procprefixes[do_read]+'float';
  358. readfunctype:=pbestrealtype^;
  359. end;
  360. end;
  361. enumdef:
  362. begin
  363. name:=procprefixes[do_read]+'enum';
  364. readfunctype:=s32inttype;
  365. end;
  366. orddef :
  367. begin
  368. case Torddef(para.left.resultdef).ordtype of
  369. {$ifdef cpu64bit}
  370. s64bit,
  371. {$endif cpu64bit}
  372. s8bit,
  373. s16bit,
  374. s32bit :
  375. begin
  376. name := procprefixes[do_read]+'sint';
  377. readfunctype:=sinttype;
  378. end;
  379. {$ifdef cpu64bit}
  380. u64bit,
  381. {$endif cpu64bit}
  382. u8bit,
  383. u16bit,
  384. u32bit :
  385. begin
  386. name := procprefixes[do_read]+'uint';
  387. readfunctype:=uinttype;
  388. end;
  389. uchar :
  390. begin
  391. name := procprefixes[do_read]+'char';
  392. readfunctype:=cchartype;
  393. end;
  394. uwidechar :
  395. begin
  396. name := procprefixes[do_read]+'widechar';
  397. readfunctype:=cwidechartype;
  398. end;
  399. {$ifndef cpu64bit}
  400. s64bit :
  401. begin
  402. name := procprefixes[do_read]+'int64';
  403. readfunctype:=s64inttype;
  404. end;
  405. u64bit :
  406. begin
  407. name := procprefixes[do_read]+'qword';
  408. readfunctype:=u64inttype;
  409. end;
  410. {$endif cpu64bit}
  411. scurrency:
  412. begin
  413. name := procprefixes[do_read]+'currency';
  414. readfunctype:=s64currencytype;
  415. is_real:=true;
  416. end;
  417. bool8bit,
  418. bool16bit,
  419. bool32bit,
  420. bool64bit:
  421. if do_read then
  422. begin
  423. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  424. error_para := true;
  425. end
  426. else
  427. begin
  428. name := procprefixes[do_read]+'boolean';
  429. readfunctype:=booltype;
  430. end
  431. else
  432. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  433. error_para := true;
  434. end;
  435. end;
  436. variantdef :
  437. name:=procprefixes[do_read]+'variant';
  438. arraydef :
  439. begin
  440. if is_chararray(para.left.resultdef) then
  441. name := procprefixes[do_read]+'pchar_as_array'
  442. else
  443. begin
  444. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  445. error_para := true;
  446. end
  447. end
  448. else
  449. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  450. error_para := true;
  451. end;
  452. { check for length/fractional colon para's }
  453. fracpara:=nil;
  454. lenpara:=nil;
  455. indexpara:=nil;
  456. if assigned(para.right) and
  457. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  458. begin
  459. lenpara := tcallparanode(para.right);
  460. if assigned(lenpara.right) and
  461. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  462. fracpara:=tcallparanode(lenpara.right);
  463. end;
  464. { get the next parameter now already, because we're going }
  465. { to muck around with the pointers }
  466. if assigned(fracpara) then
  467. nextpara := tcallparanode(fracpara.right)
  468. else if assigned(lenpara) then
  469. nextpara := tcallparanode(lenpara.right)
  470. else
  471. nextpara := tcallparanode(para.right);
  472. { check if a fracpara is allowed }
  473. if assigned(fracpara) and not is_real then
  474. begin
  475. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  476. error_para := true;
  477. end
  478. else if assigned(lenpara) and do_read then
  479. begin
  480. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  481. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  482. error_para := true;
  483. end;
  484. { adjust found_error }
  485. found_error := found_error or error_para;
  486. if not error_para then
  487. begin
  488. special_handling:=false;
  489. { create dummy frac/len para's if necessary }
  490. if not do_read then
  491. begin
  492. { difference in default value for floats and the rest :( }
  493. if not is_real then
  494. begin
  495. if not assigned(lenpara) then
  496. lenpara := ccallparanode.create(
  497. cordconstnode.create(0,s32inttype,false),nil)
  498. else
  499. { make sure we don't pass the successive }
  500. { parameters too. We also already have a }
  501. { reference to the next parameter in }
  502. { nextpara }
  503. lenpara.right := nil;
  504. end
  505. else
  506. begin
  507. if not assigned(lenpara) then
  508. lenpara := ccallparanode.create(
  509. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  510. { also create a default fracpara if necessary }
  511. if not assigned(fracpara) then
  512. fracpara := ccallparanode.create(
  513. cordconstnode.create(int64(-1),s32inttype,false),nil);
  514. { add it to the lenpara }
  515. lenpara.right := fracpara;
  516. if not is_currency(para.left.resultdef) then
  517. begin
  518. { and add the realtype para (this also removes the link }
  519. { to any parameters coming after it) }
  520. fracpara.right := ccallparanode.create(
  521. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  522. s32inttype,true),nil);
  523. end;
  524. end;
  525. if para.left.resultdef.typ=enumdef then
  526. begin
  527. {To write(ln) an enum we need a some extra parameters.}
  528. {Insert a reference to the ord2string index.}
  529. indexpara:=Ccallparanode.create(
  530. Caddrnode.create_internal(
  531. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  532. ),
  533. nil);
  534. {Insert a reference to the typinfo.}
  535. indexpara:=Ccallparanode.create(
  536. Caddrnode.create_internal(
  537. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  538. ),
  539. indexpara);
  540. {Insert a type conversion to to convert the enum to longint.}
  541. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  542. typecheckpass(para.left);
  543. end;
  544. end
  545. else
  546. begin
  547. {To read(ln) an enum we need a an extra parameter.}
  548. if para.left.resultdef.typ=enumdef then
  549. begin
  550. {Insert a reference to the string2ord index.}
  551. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  552. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  553. ),nil);
  554. {Insert a type conversion to to convert the enum to longint.}
  555. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  556. typecheckpass(para.left);
  557. end;
  558. { special handling of reading small numbers, because the helpers }
  559. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  560. { use functions because then the call to FPC_IOCHECK destroys }
  561. { their result before we can store it }
  562. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  563. special_handling:=true;
  564. end;
  565. if special_handling then
  566. begin
  567. { since we're not going to pass the parameter as var-parameter }
  568. { to the read function, manually check whether the parameter }
  569. { can be used as var-parameter (e.g., whether it isn't a }
  570. { property) }
  571. valid_for_var(para.left,true);
  572. { create the parameter list: the temp ... }
  573. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  574. addstatement(Tstatementnode(newstatement),temp);
  575. { ... and the file }
  576. p1 := ccallparanode.create(ctemprefnode.create(temp),
  577. filepara.getcopy);
  578. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  579. { create the call to the helper }
  580. addstatement(Tstatementnode(newstatement),
  581. ccallnode.createintern(name,tcallparanode(p1)));
  582. { assign the result to the original var (this automatically }
  583. { takes care of range checking) }
  584. addstatement(Tstatementnode(newstatement),
  585. cassignmentnode.create(para.left,
  586. ctemprefnode.create(temp)));
  587. { release the temp location }
  588. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  589. { statement of para is used }
  590. para.left := nil;
  591. { free the enclosing tcallparanode, but not the }
  592. { parameters coming after it }
  593. para.right := nil;
  594. para.free;
  595. end
  596. else
  597. { read of non s/u-8/16bit, or a write }
  598. begin
  599. { add the filepara to the current parameter }
  600. para.right := filepara.getcopy;
  601. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  602. already linked with the lenpara if necessary).}
  603. if indexpara=nil then
  604. Tcallparanode(para.right).right:=lenpara
  605. else
  606. begin
  607. if lenpara=nil then
  608. Tcallparanode(para.right).right:=indexpara
  609. else
  610. begin
  611. Tcallparanode(para.right).right:=lenpara;
  612. lenpara.right:=indexpara;
  613. end;
  614. { indexpara.right:=lenpara;}
  615. end;
  616. { in case of writing a chararray, add whether it's }
  617. { zero-based }
  618. if para.left.resultdef.typ=arraydef then
  619. para := ccallparanode.create(cordconstnode.create(
  620. ord(tarraydef(para.left.resultdef).lowrange=0),booltype,false),para);
  621. { create the call statement }
  622. addstatement(Tstatementnode(newstatement),
  623. ccallnode.createintern(name,para));
  624. end
  625. end
  626. else
  627. { error_para = true }
  628. begin
  629. { free the parameter, since it isn't referenced anywhere anymore }
  630. para.right := nil;
  631. para.free;
  632. if assigned(lenpara) then
  633. begin
  634. lenpara.right := nil;
  635. lenpara.free;
  636. end;
  637. if assigned(fracpara) then
  638. begin
  639. fracpara.right := nil;
  640. fracpara.free;
  641. end;
  642. end;
  643. { process next parameter }
  644. para := nextpara;
  645. end;
  646. { if no error, add the write(ln)/read(ln) end calls }
  647. if not found_error then
  648. begin
  649. case inlinenumber of
  650. in_read_x,
  651. in_readstr_x:
  652. name:='fpc_read_end';
  653. in_write_x,
  654. in_writestr_x:
  655. name:='fpc_write_end';
  656. in_readln_x:
  657. name:='fpc_readln_end';
  658. in_writeln_x:
  659. name:='fpc_writeln_end';
  660. end;
  661. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara));
  662. end;
  663. handle_text_read_write:=found_error;
  664. end;
  665. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  666. {Read/write for typed files.}
  667. const procprefixes:array[boolean] of string[15]=('fpc_typed_write','fpc_typed_read');
  668. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  669. var found_error,do_read,is_rwstr:boolean;
  670. para,nextpara:Tcallparanode;
  671. p1:Tnode;
  672. temp:Ttempcreatenode;
  673. begin
  674. found_error:=false;
  675. para:=Tcallparanode(params);
  676. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  677. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  678. { add the typesize to the filepara }
  679. if filepara.resultdef.typ=filedef then
  680. filepara.right := ccallparanode.create(cordconstnode.create(
  681. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  682. { check for "no parameters" (you need at least one extra para for typed files) }
  683. if not assigned(para) then
  684. begin
  685. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  686. found_error := true;
  687. end;
  688. { process all parameters }
  689. while assigned(para) do
  690. begin
  691. { check if valid parameter }
  692. if para.left.nodetype=typen then
  693. begin
  694. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  695. found_error := true;
  696. end;
  697. { support writeln(procvar) }
  698. if (para.left.resultdef.typ=procvardef) then
  699. begin
  700. p1:=ccallnode.create_procvar(nil,para.left);
  701. typecheckpass(p1);
  702. para.left:=p1;
  703. end;
  704. if filepara.resultdef.typ=filedef then
  705. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  706. if assigned(para.right) and
  707. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  708. begin
  709. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  710. { skip all colon para's }
  711. nextpara := tcallparanode(tcallparanode(para.right).right);
  712. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  713. nextpara := tcallparanode(nextpara.right);
  714. found_error := true;
  715. end
  716. else
  717. { get next parameter }
  718. nextpara := tcallparanode(para.right);
  719. { When we have a call, we have a problem: you can't pass the }
  720. { result of a call as a formal const parameter. Solution: }
  721. { assign the result to a temp and pass this temp as parameter }
  722. { This is not very efficient, but write(typedfile,x) is }
  723. { already slow by itself anyway (no buffering) (JM) }
  724. { Actually, thge same goes for every non-simple expression }
  725. { (such as an addition, ...) -> put everything but load nodes }
  726. { into temps (JM) }
  727. { of course, this must only be allowed for writes!!! (JM) }
  728. if not(do_read) and (para.left.nodetype <> loadn) then
  729. begin
  730. { create temp for result }
  731. temp := ctempcreatenode.create(para.left.resultdef,
  732. para.left.resultdef.size,tt_persistent,false);
  733. addstatement(Tstatementnode(newstatement),temp);
  734. { assign result to temp }
  735. addstatement(Tstatementnode(newstatement),
  736. cassignmentnode.create(ctemprefnode.create(temp),
  737. para.left));
  738. { replace (reused) paranode with temp }
  739. para.left := ctemprefnode.create(temp);
  740. end;
  741. { add fileparameter }
  742. para.right := filepara.getcopy;
  743. { create call statment }
  744. { since the parameters are in the correct order, we have to insert }
  745. { the statements always at the end of the current block }
  746. addstatement(Tstatementnode(newstatement),
  747. Ccallnode.createintern(procprefixes[do_read],para
  748. ));
  749. { if we used a temp, free it }
  750. if para.left.nodetype = temprefn then
  751. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  752. { process next parameter }
  753. para := nextpara;
  754. end;
  755. { free the file parameter }
  756. filepara.free;
  757. handle_typed_read_write:=found_error;
  758. end;
  759. function tinlinenode.handle_read_write: tnode;
  760. var
  761. filepara,
  762. nextpara,
  763. params : tcallparanode;
  764. newstatement : tstatementnode;
  765. newblock : tblocknode;
  766. filetemp : Ttempcreatenode;
  767. name : string[31];
  768. textsym : ttypesym;
  769. is_typed,
  770. do_read,
  771. is_rwstr,
  772. found_error : boolean;
  773. begin
  774. filepara := nil;
  775. is_typed := false;
  776. filetemp := nil;
  777. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  778. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  779. { if we fail, we can quickly exit this way. We must generate something }
  780. { instead of the inline node, because firstpass will bomb with an }
  781. { internalerror if it encounters a read/write }
  782. result := cerrornode.create;
  783. { reverse the parameters (needed to get the colon parameters in the }
  784. { correct order when processing write(ln) }
  785. left := reverseparameters(tcallparanode(left));
  786. if is_rwstr then
  787. begin
  788. filepara := tcallparanode(left);
  789. { needs at least two parameters: source/dest string + min. 1 value }
  790. if not(assigned(filepara)) or
  791. not(assigned(filepara.right)) then
  792. begin
  793. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  794. exit;
  795. end
  796. else if (filepara.resultdef.typ <> stringdef) then
  797. begin
  798. { convert chararray to string, or give an appropriate error message }
  799. { (if you want to optimize to use shortstring, keep in mind that }
  800. { readstr internally always uses ansistring, and to account for }
  801. { chararrays with > 255 characters) }
  802. inserttypeconv(filepara.left,cansistringtype);
  803. filepara.resultdef:=filepara.left.resultdef;
  804. if codegenerror then
  805. exit;
  806. end
  807. end
  808. else if assigned(left) then
  809. begin
  810. { check if we have a file parameter and if yes, what kind it is }
  811. filepara := tcallparanode(left);
  812. if (filepara.resultdef.typ=filedef) then
  813. begin
  814. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  815. begin
  816. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  817. exit;
  818. end
  819. else
  820. begin
  821. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  822. begin
  823. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  824. begin
  825. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  826. exit;
  827. end;
  828. is_typed := true;
  829. end
  830. end;
  831. end
  832. else
  833. filepara := nil;
  834. end;
  835. { create a blocknode in which the successive write/read statements will be }
  836. { put, since they belong together. Also create a dummy statement already to }
  837. { make inserting of additional statements easier }
  838. newblock:=internalstatements(newstatement);
  839. { if we don't have a filepara, create one containing the default }
  840. if not assigned(filepara) or
  841. is_rwstr then
  842. begin
  843. { since the input/output variables are threadvars loading them into
  844. a temp once is faster. Create a temp which will hold a pointer to the file }
  845. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  846. addstatement(newstatement,filetemp);
  847. { make sure the resultdef of the temp (and as such of the }
  848. { temprefs coming after it) is set (necessary because the }
  849. { temprefs will be part of the filepara, of which we need }
  850. { the resultdef later on and temprefs can only be }
  851. { typecheckpassed if the resultdef of the temp is known) }
  852. typecheckpass(tnode(filetemp));
  853. if not is_rwstr then
  854. begin
  855. { assign the address of the file to the temp }
  856. if do_read then
  857. name := 'input'
  858. else
  859. name := 'output';
  860. addstatement(newstatement,
  861. cassignmentnode.create(ctemprefnode.create(filetemp),
  862. ccallnode.createintern('fpc_get_'+name,nil)));
  863. end
  864. else
  865. begin
  866. if (do_read) then
  867. name := 'fpc_setupreadstr_'
  868. else
  869. name := 'fpc_setupwritestr_';
  870. name:=name+tstringdef(filepara.resultdef).stringtypname;
  871. { remove the source/destination string parameter from the }
  872. { parameter chain }
  873. left:=filepara.right;
  874. filepara.right:=nil;
  875. { pass the source/destination string to the setup routine, which }
  876. { will store the string's address in the returned textrec }
  877. addstatement(newstatement,
  878. cassignmentnode.create(ctemprefnode.create(filetemp),
  879. ccallnode.createintern(name,filepara)));
  880. end;
  881. { create a new fileparameter as follows: file_type(temp^) }
  882. { (so that we pass the value and not the address of the temp }
  883. { to the read/write routine) }
  884. textsym:=search_system_type('TEXT');
  885. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  886. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  887. end
  888. else
  889. { remove filepara from the parameter chain }
  890. begin
  891. left := filepara.right;
  892. filepara.right := nil;
  893. { the file para is a var parameter, but it must be valid already }
  894. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  895. { check if we should make a temp to store the result of a complex }
  896. { expression (better heuristics, anyone?) (JM) }
  897. if (filepara.left.nodetype <> loadn) then
  898. begin
  899. { create a temp which will hold a pointer to the file }
  900. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  901. { add it to the statements }
  902. addstatement(newstatement,filetemp);
  903. { make sure the resultdef of the temp (and as such of the }
  904. { temprefs coming after it) is set (necessary because the }
  905. { temprefs will be part of the filepara, of which we need }
  906. { the resultdef later on and temprefs can only be }
  907. { typecheckpassed if the resultdef of the temp is known) }
  908. typecheckpass(tnode(filetemp));
  909. { assign the address of the file to the temp }
  910. addstatement(newstatement,
  911. cassignmentnode.create(ctemprefnode.create(filetemp),
  912. caddrnode.create_internal(filepara.left)));
  913. typecheckpass(newstatement.left);
  914. { create a new fileparameter as follows: file_type(temp^) }
  915. { (so that we pass the value and not the address of the temp }
  916. { to the read/write routine) }
  917. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  918. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  919. { replace the old file para with the new one }
  920. filepara.left := nil;
  921. filepara.free;
  922. filepara := nextpara;
  923. end;
  924. end;
  925. { the resultdef of the filepara must be set since it's }
  926. { used below }
  927. filepara.get_paratype;
  928. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  929. { if something goes wrong or at the end of the procedure }
  930. { we're going to reuse the paranodes, so make sure they don't get freed }
  931. { twice }
  932. params:=Tcallparanode(left);
  933. left := nil;
  934. if is_typed then
  935. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),newstatement)
  936. else
  937. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),newstatement);
  938. { if we found an error, simply delete the generated blocknode }
  939. if found_error then
  940. newblock.free
  941. else
  942. begin
  943. { deallocate the temp for the file para if we used one }
  944. if assigned(filetemp) then
  945. addstatement(newstatement,ctempdeletenode.create(filetemp));
  946. { otherwise return the newly generated block of instructions, }
  947. { but first free the errornode we generated at the beginning }
  948. result.free;
  949. result := newblock
  950. end;
  951. end;
  952. function tinlinenode.handle_val: tnode;
  953. var
  954. procname,
  955. suffix : string[31];
  956. sourcepara,
  957. destpara,
  958. codepara,
  959. sizepara,
  960. newparas : tcallparanode;
  961. orgcode,tc : tnode;
  962. newstatement : tstatementnode;
  963. newblock : tblocknode;
  964. tempcode : ttempcreatenode;
  965. begin
  966. { for easy exiting if something goes wrong }
  967. result := cerrornode.create;
  968. { check the amount of parameters }
  969. if not(assigned(left)) or
  970. not(assigned(tcallparanode(left).right)) then
  971. begin
  972. CGMessage1(parser_e_wrong_parameter_size,'Val');
  973. exit;
  974. end;
  975. { reverse parameters for easier processing }
  976. left := reverseparameters(tcallparanode(left));
  977. { get the parameters }
  978. tempcode := nil;
  979. orgcode := nil;
  980. sizepara := nil;
  981. sourcepara := tcallparanode(left);
  982. destpara := tcallparanode(sourcepara.right);
  983. codepara := tcallparanode(destpara.right);
  984. { check if codepara is valid }
  985. if assigned(codepara) and
  986. (
  987. (codepara.resultdef.typ <> orddef)
  988. {$ifndef cpu64bit}
  989. or is_64bitint(codepara.resultdef)
  990. {$endif cpu64bit}
  991. ) then
  992. begin
  993. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  994. exit;
  995. end;
  996. { check if dest para is valid }
  997. if not(destpara.resultdef.typ in [orddef,floatdef,enumdef]) then
  998. begin
  999. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1000. exit;
  1001. end;
  1002. { we're going to reuse the exisiting para's, so make sure they }
  1003. { won't be disposed }
  1004. left := nil;
  1005. { create the blocknode which will hold the generated statements + }
  1006. { an initial dummy statement }
  1007. newblock:=internalstatements(newstatement);
  1008. { do we need a temp for code? Yes, if no code specified, or if }
  1009. { code is not a 32bit parameter (we already checked whether the }
  1010. { the code para, if specified, was an orddef) }
  1011. if not assigned(codepara) or
  1012. (codepara.resultdef.size<>sinttype.size) then
  1013. begin
  1014. tempcode := ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1015. addstatement(newstatement,tempcode);
  1016. { set the resultdef of the temp (needed to be able to get }
  1017. { the resultdef of the tempref used in the new code para) }
  1018. typecheckpass(tnode(tempcode));
  1019. { create a temp codepara, but save the original code para to }
  1020. { assign the result to later on }
  1021. if assigned(codepara) then
  1022. begin
  1023. orgcode := codepara.left;
  1024. codepara.left := ctemprefnode.create(tempcode);
  1025. end
  1026. else
  1027. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1028. { we need its resultdef later on }
  1029. codepara.get_paratype;
  1030. end
  1031. else if (torddef(codepara.resultdef).ordtype = torddef(sinttype).ordtype) then
  1032. { because code is a var parameter, it must match types exactly }
  1033. { however, since it will return values in [0..255], both longints }
  1034. { and cardinals are fine. Since the formal code para type is }
  1035. { longint, insert a typecoversion to longint for cardinal para's }
  1036. begin
  1037. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  1038. { make it explicit, oterwise you may get a nonsense range }
  1039. { check error if the cardinal already contained a value }
  1040. { > $7fffffff }
  1041. codepara.get_paratype;
  1042. end;
  1043. { create the procedure name }
  1044. procname := 'fpc_val_';
  1045. case destpara.resultdef.typ of
  1046. orddef:
  1047. begin
  1048. case torddef(destpara.resultdef).ordtype of
  1049. {$ifdef cpu64bit}
  1050. s64bit,
  1051. {$endif cpu64bit}
  1052. s8bit,
  1053. s16bit,
  1054. s32bit:
  1055. begin
  1056. suffix := 'sint_';
  1057. { we also need a destsize para in this case }
  1058. sizepara := ccallparanode.create(cordconstnode.create
  1059. (destpara.resultdef.size,s32inttype,true),nil);
  1060. end;
  1061. {$ifdef cpu64bit}
  1062. u64bit,
  1063. {$endif cpu64bit}
  1064. u8bit,
  1065. u16bit,
  1066. u32bit:
  1067. suffix := 'uint_';
  1068. {$ifndef cpu64bit}
  1069. s64bit: suffix := 'int64_';
  1070. u64bit: suffix := 'qword_';
  1071. {$endif cpu64bit}
  1072. scurrency: suffix := 'currency_';
  1073. else
  1074. internalerror(200304225);
  1075. end;
  1076. end;
  1077. floatdef:
  1078. suffix:='real_';
  1079. enumdef:
  1080. begin
  1081. suffix:='enum_';
  1082. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1083. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1084. ),nil);
  1085. end;
  1086. end;
  1087. procname := procname + suffix;
  1088. { play a trick to have tcallnode handle invalid source parameters: }
  1089. { the shortstring-longint val routine by default }
  1090. if (sourcepara.resultdef.typ = stringdef) then
  1091. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1092. { zero-based arrays (of char) can be implicitely converted to ansistring }
  1093. else if is_zero_based_array(sourcepara.resultdef) then
  1094. procname := procname + 'ansistr'
  1095. else
  1096. procname := procname + 'shortstr';
  1097. { set up the correct parameters for the call: the code para... }
  1098. newparas := codepara;
  1099. { and the source para }
  1100. codepara.right := sourcepara;
  1101. { sizepara either contains nil if none is needed (which is ok, since }
  1102. { then the next statement severes any possible links with other paras }
  1103. { that sourcepara may have) or it contains the necessary size para and }
  1104. { its right field is nil }
  1105. sourcepara.right := sizepara;
  1106. { create the call and assign the result to dest (val helpers are functions).
  1107. Use a trick to prevent a type size mismatch warning to be generated by the
  1108. assignment node. First convert implicitly to the resultdef. This will insert
  1109. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1110. for the assignment node and therefor preventing the warning (PFV)
  1111. The implicit conversion is avoided for enums because implicit conversion between
  1112. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1113. possible. (DM).}
  1114. if destpara.resultdef.typ=enumdef then
  1115. tc:=ccallnode.createintern(procname,newparas)
  1116. else
  1117. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1118. addstatement(newstatement,cassignmentnode.create(
  1119. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1120. { dispose of the enclosing paranode of the destination }
  1121. destpara.left := nil;
  1122. destpara.right := nil;
  1123. destpara.free;
  1124. { check if we used a temp for code and whether we have to store }
  1125. { it to the real code parameter }
  1126. if assigned(orgcode) then
  1127. addstatement(newstatement,cassignmentnode.create(
  1128. orgcode,
  1129. ctypeconvnode.create_internal(
  1130. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1131. { release the temp if we allocated one }
  1132. if assigned(tempcode) then
  1133. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1134. { free the errornode }
  1135. result.free;
  1136. { and return it }
  1137. result := newblock;
  1138. end;
  1139. {$maxfpuregisters 0}
  1140. function getpi : bestreal;
  1141. begin
  1142. {$ifdef x86}
  1143. { x86 has pi in hardware }
  1144. result:=pi;
  1145. {$else x86}
  1146. {$ifdef cpuextended}
  1147. result:=MathPiExtended.Value;
  1148. {$else cpuextended}
  1149. result:=MathPi.Value;
  1150. {$endif cpuextended}
  1151. {$endif x86}
  1152. end;
  1153. function tinlinenode.pass_typecheck:tnode;
  1154. function do_lowhigh(def:tdef) : tnode;
  1155. var
  1156. v : tconstexprint;
  1157. enum : tenumsym;
  1158. hp : tnode;
  1159. begin
  1160. case def.typ of
  1161. orddef:
  1162. begin
  1163. set_varstate(left,vs_read,[]);
  1164. if inlinenumber=in_low_x then
  1165. v:=torddef(def).low
  1166. else
  1167. v:=torddef(def).high;
  1168. hp:=cordconstnode.create(v,def,true);
  1169. typecheckpass(hp);
  1170. do_lowhigh:=hp;
  1171. end;
  1172. enumdef:
  1173. begin
  1174. set_varstate(left,vs_read,[]);
  1175. enum:=tenumsym(tenumdef(def).firstenum);
  1176. v:=tenumdef(def).maxval;
  1177. if inlinenumber=in_high_x then
  1178. while assigned(enum) and (enum.value <> v) do
  1179. enum:=enum.nextenum;
  1180. if not assigned(enum) then
  1181. internalerror(309993)
  1182. else
  1183. hp:=genenumnode(enum);
  1184. do_lowhigh:=hp;
  1185. end;
  1186. else
  1187. internalerror(87);
  1188. end;
  1189. end;
  1190. function getconstrealvalue : bestreal;
  1191. begin
  1192. case left.nodetype of
  1193. ordconstn:
  1194. getconstrealvalue:=tordconstnode(left).value;
  1195. realconstn:
  1196. getconstrealvalue:=trealconstnode(left).value_real;
  1197. else
  1198. internalerror(309992);
  1199. end;
  1200. end;
  1201. procedure setconstrealvalue(r : bestreal);
  1202. begin
  1203. result:=crealconstnode.create(r,pbestrealtype^);
  1204. end;
  1205. function handle_ln_const(r : bestreal) : tnode;
  1206. begin
  1207. if r<=0.0 then
  1208. if (cs_check_range in current_settings.localswitches) or
  1209. (cs_check_overflow in current_settings.localswitches) then
  1210. begin
  1211. result:=crealconstnode.create(0,pbestrealtype^);
  1212. CGMessage(type_e_wrong_math_argument)
  1213. end
  1214. else
  1215. begin
  1216. if r=0.0 then
  1217. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1218. else
  1219. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1220. end
  1221. else
  1222. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1223. end;
  1224. function handle_sqrt_const(r : bestreal) : tnode;
  1225. begin
  1226. if r<0.0 then
  1227. if (cs_check_range in current_settings.localswitches) or
  1228. (cs_check_overflow in current_settings.localswitches) then
  1229. begin
  1230. result:=crealconstnode.create(0,pbestrealtype^);
  1231. CGMessage(type_e_wrong_math_argument)
  1232. end
  1233. else
  1234. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1235. else
  1236. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1237. end;
  1238. procedure setfloatresultdef;
  1239. begin
  1240. if (left.resultdef.typ=floatdef) and
  1241. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1242. resultdef:=left.resultdef
  1243. else
  1244. begin
  1245. inserttypeconv(left,pbestrealtype^);
  1246. resultdef:=pbestrealtype^;
  1247. end;
  1248. end;
  1249. procedure handle_pack_unpack;
  1250. var
  1251. source, target, index: tcallparanode;
  1252. unpackedarraydef, packedarraydef: tarraydef;
  1253. tempindex: TConstExprInt;
  1254. begin
  1255. resultdef:=voidtype;
  1256. unpackedarraydef := nil;
  1257. packedarraydef := nil;
  1258. source := tcallparanode(left);
  1259. if (inlinenumber = in_unpack_x_y_z) then
  1260. begin
  1261. target := tcallparanode(source.right);
  1262. index := tcallparanode(target.right);
  1263. { source must be a packed array }
  1264. if not is_packed_array(source.left.resultdef) then
  1265. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1266. else
  1267. packedarraydef := tarraydef(source.left.resultdef);
  1268. { target can be any kind of array, as long as it's not packed }
  1269. if (target.left.resultdef.typ <> arraydef) or
  1270. is_packed_array(target.left.resultdef) then
  1271. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1272. else
  1273. unpackedarraydef := tarraydef(target.left.resultdef);
  1274. end
  1275. else
  1276. begin
  1277. index := tcallparanode(source.right);
  1278. target := tcallparanode(index.right);
  1279. { source can be any kind of array, as long as it's not packed }
  1280. if (source.left.resultdef.typ <> arraydef) or
  1281. is_packed_array(source.left.resultdef) then
  1282. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1283. else
  1284. unpackedarraydef := tarraydef(source.left.resultdef);
  1285. { target must be a packed array }
  1286. if not is_packed_array(target.left.resultdef) then
  1287. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1288. else
  1289. packedarraydef := tarraydef(target.left.resultdef);
  1290. end;
  1291. if assigned(unpackedarraydef) then
  1292. begin
  1293. { index must be compatible with the unpacked array's indextype }
  1294. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1295. { range check at compile time if possible }
  1296. if assigned(packedarraydef) and
  1297. (index.left.nodetype = ordconstn) and
  1298. not is_special_array(unpackedarraydef) then
  1299. begin
  1300. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1301. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1302. testrange(unpackedarraydef,tempindex,false);
  1303. end;
  1304. end;
  1305. { source array is read and must be valid }
  1306. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1307. { target array is written }
  1308. valid_for_assignment(target.left,true);
  1309. set_varstate(target.left,vs_written,[]);
  1310. { index in the unpacked array is read and must be valid }
  1311. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1312. { if the size of the arrays is 0 (array of empty records), }
  1313. { do nothing }
  1314. if (source.resultdef.size = 0) then
  1315. result:=cnothingnode.create;
  1316. end;
  1317. var
  1318. vl,vl2 : TConstExprInt;
  1319. vr : bestreal;
  1320. hightree,
  1321. hp : tnode;
  1322. checkrange : boolean;
  1323. label
  1324. myexit;
  1325. begin
  1326. result:=nil;
  1327. { if we handle writeln; left contains no valid address }
  1328. if assigned(left) then
  1329. begin
  1330. if left.nodetype=callparan then
  1331. tcallparanode(left).get_paratype
  1332. else
  1333. typecheckpass(left);
  1334. end;
  1335. inc(parsing_para_level);
  1336. { handle intern constant functions in separate case }
  1337. if nf_inlineconst in flags then
  1338. begin
  1339. { no parameters? }
  1340. if not assigned(left) then
  1341. internalerror(200501231)
  1342. else
  1343. begin
  1344. vl:=0;
  1345. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1346. case left.nodetype of
  1347. realconstn :
  1348. begin
  1349. { Real functions are all handled with internproc below }
  1350. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1351. end;
  1352. ordconstn :
  1353. vl:=tordconstnode(left).value;
  1354. callparan :
  1355. begin
  1356. { both exists, else it was not generated }
  1357. vl:=tordconstnode(tcallparanode(left).left).value;
  1358. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1359. end;
  1360. else
  1361. CGMessage(parser_e_illegal_expression);
  1362. end;
  1363. case inlinenumber of
  1364. in_const_abs :
  1365. if vl.signed then
  1366. hp:=genintconstnode(abs(vl.svalue))
  1367. else
  1368. hp:=genintconstnode(vl.uvalue);
  1369. in_const_sqr:
  1370. if vl.signed then
  1371. hp:=genintconstnode(sqr(vl.svalue))
  1372. else
  1373. hp:=genintconstnode(sqr(vl.uvalue));
  1374. in_const_odd :
  1375. hp:=cordconstnode.create(qword(odd(int64(vl))),booltype,true);
  1376. in_const_swap_word :
  1377. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1378. in_const_swap_long :
  1379. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1380. in_const_swap_qword :
  1381. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1382. in_const_ptr:
  1383. begin
  1384. {Don't construct pointers from negative values.}
  1385. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1386. cgmessage(parser_e_range_check_error);
  1387. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1388. end
  1389. else
  1390. internalerror(88);
  1391. end;
  1392. end;
  1393. if hp=nil then
  1394. hp:=cerrornode.create;
  1395. result:=hp;
  1396. goto myexit;
  1397. end
  1398. else
  1399. begin
  1400. case inlinenumber of
  1401. in_lo_long,
  1402. in_hi_long,
  1403. in_lo_qword,
  1404. in_hi_qword,
  1405. in_lo_word,
  1406. in_hi_word :
  1407. begin
  1408. { give warning for incompatibility with tp and delphi }
  1409. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1410. ((m_tp7 in current_settings.modeswitches) or
  1411. (m_delphi in current_settings.modeswitches)) then
  1412. CGMessage(type_w_maybe_wrong_hi_lo);
  1413. { constant folding }
  1414. if left.nodetype=ordconstn then
  1415. begin
  1416. case inlinenumber of
  1417. in_lo_word :
  1418. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resultdef,true);
  1419. in_hi_word :
  1420. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resultdef,true);
  1421. in_lo_long :
  1422. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resultdef,true);
  1423. in_hi_long :
  1424. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resultdef,true);
  1425. in_lo_qword :
  1426. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resultdef,true);
  1427. in_hi_qword :
  1428. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resultdef,true);
  1429. end;
  1430. result:=hp;
  1431. goto myexit;
  1432. end;
  1433. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1434. if not is_integer(left.resultdef) then
  1435. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1436. case inlinenumber of
  1437. in_lo_word,
  1438. in_hi_word :
  1439. resultdef:=u8inttype;
  1440. in_lo_long,
  1441. in_hi_long :
  1442. resultdef:=u16inttype;
  1443. in_lo_qword,
  1444. in_hi_qword :
  1445. resultdef:=u32inttype;
  1446. end;
  1447. end;
  1448. in_sizeof_x:
  1449. begin
  1450. set_varstate(left,vs_read,[]);
  1451. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1452. begin
  1453. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1454. if assigned(hightree) then
  1455. begin
  1456. hp:=caddnode.create(addn,hightree,
  1457. cordconstnode.create(1,sinttype,false));
  1458. if (left.resultdef.typ=arraydef) then
  1459. if not is_packed_array(tarraydef(left.resultdef)) then
  1460. begin
  1461. if (tarraydef(left.resultdef).elesize<>1) then
  1462. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1463. left.resultdef).elesize,sinttype,true));
  1464. end
  1465. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1466. begin
  1467. { no packed open array support yet }
  1468. if (hp.nodetype <> ordconstn) then
  1469. internalerror(2006081511);
  1470. hp.free;
  1471. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1472. {
  1473. hp:=
  1474. ctypeconvnode.create_explicit(sinttype,
  1475. cmoddivnode.create(divn,
  1476. caddnode.create(addn,
  1477. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1478. left.resultdef).elepackedbitsize,s64inttype,true)),
  1479. cordconstnode.create(a,s64inttype,true)),
  1480. cordconstnode.create(8,s64inttype,true)),
  1481. sinttype);
  1482. }
  1483. end;
  1484. result:=hp;
  1485. end;
  1486. end
  1487. else
  1488. resultdef:=sinttype;
  1489. end;
  1490. in_typeof_x:
  1491. begin
  1492. set_varstate(left,vs_read,[]);
  1493. resultdef:=voidpointertype;
  1494. end;
  1495. in_ord_x:
  1496. begin
  1497. if (left.nodetype=ordconstn) then
  1498. begin
  1499. hp:=cordconstnode.create(
  1500. tordconstnode(left).value,sinttype,true);
  1501. result:=hp;
  1502. goto myexit;
  1503. end;
  1504. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1505. case left.resultdef.typ of
  1506. orddef :
  1507. begin
  1508. case torddef(left.resultdef).ordtype of
  1509. bool8bit,
  1510. uchar:
  1511. begin
  1512. { change to byte() }
  1513. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1514. left:=nil;
  1515. result:=hp;
  1516. end;
  1517. bool16bit,
  1518. uwidechar :
  1519. begin
  1520. { change to word() }
  1521. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1522. left:=nil;
  1523. result:=hp;
  1524. end;
  1525. bool32bit :
  1526. begin
  1527. { change to dword() }
  1528. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1529. left:=nil;
  1530. result:=hp;
  1531. end;
  1532. bool64bit :
  1533. begin
  1534. { change to qword() }
  1535. hp:=ctypeconvnode.create_internal(left,u64inttype);
  1536. left:=nil;
  1537. result:=hp;
  1538. end;
  1539. uvoid :
  1540. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1541. else
  1542. begin
  1543. { all other orddef need no transformation }
  1544. hp:=left;
  1545. left:=nil;
  1546. result:=hp;
  1547. end;
  1548. end;
  1549. end;
  1550. enumdef :
  1551. begin
  1552. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1553. left:=nil;
  1554. result:=hp;
  1555. end;
  1556. pointerdef :
  1557. begin
  1558. if m_mac in current_settings.modeswitches then
  1559. begin
  1560. hp:=ctypeconvnode.create_internal(left,ptruinttype);
  1561. left:=nil;
  1562. result:=hp;
  1563. end
  1564. else
  1565. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1566. end
  1567. else
  1568. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1569. end;
  1570. end;
  1571. in_chr_byte:
  1572. begin
  1573. { convert to explicit char() }
  1574. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1575. hp:=ctypeconvnode.create_internal(left,cchartype);
  1576. left:=nil;
  1577. result:=hp;
  1578. end;
  1579. in_length_x:
  1580. begin
  1581. if ((left.resultdef.typ=arraydef) and
  1582. (not is_special_array(left.resultdef) or
  1583. is_open_array(left.resultdef))) or
  1584. (left.resultdef.typ=orddef) then
  1585. set_varstate(left,vs_read,[])
  1586. else
  1587. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1588. case left.resultdef.typ of
  1589. variantdef:
  1590. begin
  1591. inserttypeconv(left,cansistringtype);
  1592. end;
  1593. stringdef :
  1594. begin
  1595. { we don't need string convertions here }
  1596. if (left.nodetype=typeconvn) and
  1597. (ttypeconvnode(left).left.resultdef.typ=stringdef) then
  1598. begin
  1599. hp:=ttypeconvnode(left).left;
  1600. ttypeconvnode(left).left:=nil;
  1601. left.free;
  1602. left:=hp;
  1603. end;
  1604. { evaluates length of constant strings direct }
  1605. if (left.nodetype=stringconstn) then
  1606. begin
  1607. hp:=cordconstnode.create(
  1608. tstringconstnode(left).len,s32inttype,true);
  1609. result:=hp;
  1610. goto myexit;
  1611. end;
  1612. end;
  1613. orddef :
  1614. begin
  1615. { length of char is one allways }
  1616. if is_char(left.resultdef) or
  1617. is_widechar(left.resultdef) then
  1618. begin
  1619. hp:=cordconstnode.create(1,s32inttype,false);
  1620. result:=hp;
  1621. goto myexit;
  1622. end
  1623. else
  1624. CGMessage(type_e_mismatch);
  1625. end;
  1626. pointerdef :
  1627. begin
  1628. if is_pchar(left.resultdef) then
  1629. begin
  1630. hp := ccallparanode.create(left,nil);
  1631. result := ccallnode.createintern('fpc_pchar_length',hp);
  1632. { make sure the left node doesn't get disposed, since it's }
  1633. { reused in the new node (JM) }
  1634. left:=nil;
  1635. goto myexit;
  1636. end
  1637. else if is_pwidechar(left.resultdef) then
  1638. begin
  1639. hp := ccallparanode.create(left,nil);
  1640. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1641. { make sure the left node doesn't get disposed, since it's }
  1642. { reused in the new node (JM) }
  1643. left:=nil;
  1644. goto myexit;
  1645. end
  1646. else
  1647. CGMessage(type_e_mismatch);
  1648. end;
  1649. arraydef :
  1650. begin
  1651. if is_open_array(left.resultdef) or
  1652. is_array_of_const(left.resultdef) then
  1653. begin
  1654. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1655. if assigned(hightree) then
  1656. begin
  1657. hp:=caddnode.create(addn,hightree,
  1658. cordconstnode.create(1,s32inttype,false));
  1659. result:=hp;
  1660. end;
  1661. goto myexit;
  1662. end
  1663. else
  1664. if not is_dynamic_array(left.resultdef) then
  1665. begin
  1666. hp:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1667. tarraydef(left.resultdef).lowrange+1,
  1668. s32inttype,true);
  1669. result:=hp;
  1670. goto myexit;
  1671. end
  1672. else
  1673. begin
  1674. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1675. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1676. { make sure the left node doesn't get disposed, since it's }
  1677. { reused in the new node (JM) }
  1678. left:=nil;
  1679. goto myexit;
  1680. end;
  1681. end;
  1682. else
  1683. CGMessage(type_e_mismatch);
  1684. end;
  1685. { shortstring return an 8 bit value as the length
  1686. is the first byte of the string }
  1687. if is_shortstring(left.resultdef) then
  1688. resultdef:=u8inttype
  1689. else
  1690. resultdef:=sinttype;
  1691. end;
  1692. in_typeinfo_x:
  1693. begin
  1694. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1695. resultdef:=voidpointertype;
  1696. end;
  1697. in_assigned_x:
  1698. begin
  1699. { the parser has already made sure the expression is valid }
  1700. { handle constant expressions }
  1701. if is_constnode(tcallparanode(left).left) or
  1702. (tcallparanode(left).left.nodetype = pointerconstn) then
  1703. begin
  1704. { let an add node figure it out }
  1705. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1706. tcallparanode(left).left := nil;
  1707. { free left, because otherwise some code at 'myexit' tries }
  1708. { to run get_paratype for it, which crashes since left.left }
  1709. { is now nil }
  1710. left.free;
  1711. left := nil;
  1712. goto myexit;
  1713. end;
  1714. { otherwise handle separately, because there could be a procvar, which }
  1715. { is 2*sizeof(pointer), while we must only check the first pointer }
  1716. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1717. resultdef:=booltype;
  1718. end;
  1719. in_ofs_x :
  1720. internalerror(2000101001);
  1721. in_seg_x :
  1722. begin
  1723. set_varstate(left,vs_read,[]);
  1724. result:=cordconstnode.create(0,s32inttype,false);
  1725. goto myexit;
  1726. end;
  1727. in_pred_x,
  1728. in_succ_x:
  1729. begin
  1730. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1731. resultdef:=left.resultdef;
  1732. if not is_ordinal(resultdef) then
  1733. CGMessage(type_e_ordinal_expr_expected)
  1734. else
  1735. begin
  1736. if (resultdef.typ=enumdef) and
  1737. (tenumdef(resultdef).has_jumps) and
  1738. not(m_delphi in current_settings.modeswitches) then
  1739. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1740. end;
  1741. { only if the result is an enum do we do range checking }
  1742. if (resultdef.typ=enumdef) then
  1743. checkrange := true
  1744. else
  1745. checkrange := false;
  1746. { do constant folding after check for jumps }
  1747. if left.nodetype=ordconstn then
  1748. begin
  1749. if inlinenumber=in_succ_x then
  1750. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1751. else
  1752. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1753. end;
  1754. end;
  1755. in_initialize_x,
  1756. in_finalize_x,
  1757. in_setlength_x:
  1758. begin
  1759. { inlined from pinline }
  1760. internalerror(200204231);
  1761. end;
  1762. in_inc_x,
  1763. in_dec_x:
  1764. begin
  1765. resultdef:=voidtype;
  1766. if assigned(left) then
  1767. begin
  1768. { first param must be var }
  1769. valid_for_var(tcallparanode(left).left,true);
  1770. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1771. if (left.resultdef.typ in [enumdef,pointerdef]) or
  1772. is_ordinal(left.resultdef) or
  1773. is_currency(left.resultdef) then
  1774. begin
  1775. { value of left gets changed -> must be unique }
  1776. set_unique(tcallparanode(left).left);
  1777. { two paras ? }
  1778. if assigned(tcallparanode(left).right) then
  1779. begin
  1780. if is_integer(tcallparanode(left).right.resultdef) then
  1781. begin
  1782. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1783. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  1784. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1785. { should be handled in the parser (JM) }
  1786. internalerror(2006020901);
  1787. end
  1788. else
  1789. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1790. end;
  1791. end
  1792. else
  1793. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1794. end
  1795. else
  1796. CGMessagePos(fileinfo,type_e_mismatch);
  1797. end;
  1798. in_read_x,
  1799. in_readln_x,
  1800. in_readstr_x,
  1801. in_write_x,
  1802. in_writeln_x,
  1803. in_writestr_x :
  1804. begin
  1805. result := handle_read_write;
  1806. end;
  1807. in_settextbuf_file_x :
  1808. begin
  1809. resultdef:=voidtype;
  1810. { now we know the type of buffer }
  1811. hp:=ccallparanode.create(cordconstnode.create(
  1812. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  1813. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1814. left:=nil;
  1815. end;
  1816. { the firstpass of the arg has been done in firstcalln ? }
  1817. in_reset_typedfile,
  1818. in_rewrite_typedfile :
  1819. begin
  1820. result := handle_reset_rewrite_typed;
  1821. end;
  1822. in_str_x_string :
  1823. begin
  1824. result := handle_str;
  1825. end;
  1826. in_val_x :
  1827. begin
  1828. result := handle_val;
  1829. end;
  1830. in_include_x_y,
  1831. in_exclude_x_y:
  1832. begin
  1833. resultdef:=voidtype;
  1834. { the parser already checks whether we have two (and exactly two) }
  1835. { parameters (JM) }
  1836. { first param must be var }
  1837. valid_for_var(tcallparanode(left).left,true);
  1838. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1839. { check type }
  1840. if (left.resultdef.typ=setdef) then
  1841. begin
  1842. { insert a type conversion }
  1843. { to the type of the set elements }
  1844. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1845. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1846. tsetdef(left.resultdef).elementdef);
  1847. end
  1848. else
  1849. CGMessage(type_e_mismatch);
  1850. end;
  1851. in_pack_x_y_z,
  1852. in_unpack_x_y_z :
  1853. begin
  1854. handle_pack_unpack;
  1855. end;
  1856. in_slice_x:
  1857. begin
  1858. result:=nil;
  1859. resultdef:=tcallparanode(left).left.resultdef;
  1860. if (resultdef.typ <> arraydef) then
  1861. CGMessagePos(left.fileinfo,type_e_mismatch)
  1862. else if is_packed_array(resultdef) then
  1863. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  1864. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  1865. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1866. type_e_integer_expr_expected,
  1867. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  1868. end;
  1869. in_low_x,
  1870. in_high_x:
  1871. begin
  1872. case left.resultdef.typ of
  1873. orddef,
  1874. enumdef:
  1875. begin
  1876. result:=do_lowhigh(left.resultdef);
  1877. end;
  1878. setdef:
  1879. begin
  1880. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1881. end;
  1882. arraydef:
  1883. begin
  1884. if inlinenumber=in_low_x then
  1885. begin
  1886. set_varstate(left,vs_read,[]);
  1887. result:=cordconstnode.create(int64(tarraydef(
  1888. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  1889. end
  1890. else
  1891. begin
  1892. if is_open_array(left.resultdef) or
  1893. is_array_of_const(left.resultdef) then
  1894. begin
  1895. set_varstate(left,vs_read,[]);
  1896. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1897. end
  1898. else
  1899. if is_dynamic_array(left.resultdef) then
  1900. begin
  1901. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1902. { can't use inserttypeconv because we need }
  1903. { an explicit type conversion (JM) }
  1904. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1905. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1906. { make sure the left node doesn't get disposed, since it's }
  1907. { reused in the new node (JM) }
  1908. left:=nil;
  1909. end
  1910. else
  1911. begin
  1912. set_varstate(left,vs_read,[]);
  1913. result:=cordconstnode.create(int64(tarraydef(
  1914. left.resultdef).highrange),tarraydef(left.resultdef).rangedef,true);
  1915. end;
  1916. end;
  1917. end;
  1918. stringdef:
  1919. begin
  1920. if inlinenumber=in_low_x then
  1921. begin
  1922. result:=cordconstnode.create(0,u8inttype,false);
  1923. end
  1924. else
  1925. begin
  1926. if is_open_string(left.resultdef) then
  1927. begin
  1928. set_varstate(left,vs_read,[]);
  1929. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1930. end
  1931. else
  1932. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true);
  1933. end;
  1934. end;
  1935. else
  1936. CGMessage(type_e_mismatch);
  1937. end;
  1938. end;
  1939. in_exp_real :
  1940. begin
  1941. if left.nodetype in [ordconstn,realconstn] then
  1942. begin
  1943. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1944. if (trealconstnode(result).value_real=MathInf.Value) and
  1945. ((cs_check_range in current_settings.localswitches) or
  1946. (cs_check_overflow in current_settings.localswitches)) then
  1947. begin
  1948. result:=crealconstnode.create(0,pbestrealtype^);
  1949. CGMessage(parser_e_range_check_error);
  1950. end;
  1951. end
  1952. else
  1953. begin
  1954. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1955. inserttypeconv(left,pbestrealtype^);
  1956. resultdef:=pbestrealtype^;
  1957. end;
  1958. end;
  1959. in_trunc_real :
  1960. begin
  1961. if left.nodetype in [ordconstn,realconstn] then
  1962. begin
  1963. vr:=getconstrealvalue;
  1964. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1965. begin
  1966. CGMessage(parser_e_range_check_error);
  1967. result:=cordconstnode.create(1,s64inttype,false)
  1968. end
  1969. else
  1970. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1971. end
  1972. else
  1973. begin
  1974. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1975. { for direct float rounding, no best real type cast should be necessary }
  1976. if not((left.resultdef.typ=floatdef) and
  1977. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real])) then
  1978. inserttypeconv(left,pbestrealtype^);
  1979. resultdef:=s64inttype;
  1980. end;
  1981. end;
  1982. in_round_real :
  1983. begin
  1984. if left.nodetype in [ordconstn,realconstn] then
  1985. begin
  1986. vr:=getconstrealvalue;
  1987. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1988. begin
  1989. CGMessage(parser_e_range_check_error);
  1990. result:=cordconstnode.create(1,s64inttype,false)
  1991. end
  1992. else
  1993. result:=cordconstnode.create(round(vr),s64inttype,true)
  1994. end
  1995. else
  1996. begin
  1997. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1998. { for direct float rounding, no best real type cast should be necessary }
  1999. if not((left.resultdef.typ=floatdef) and
  2000. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real])) then
  2001. inserttypeconv(left,pbestrealtype^);
  2002. resultdef:=s64inttype;
  2003. end;
  2004. end;
  2005. in_frac_real :
  2006. begin
  2007. if left.nodetype in [ordconstn,realconstn] then
  2008. setconstrealvalue(frac(getconstrealvalue))
  2009. else
  2010. begin
  2011. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2012. inserttypeconv(left,pbestrealtype^);
  2013. resultdef:=pbestrealtype^;
  2014. end;
  2015. end;
  2016. in_int_real :
  2017. begin
  2018. if left.nodetype in [ordconstn,realconstn] then
  2019. setconstrealvalue(int(getconstrealvalue))
  2020. else
  2021. begin
  2022. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2023. inserttypeconv(left,pbestrealtype^);
  2024. resultdef:=pbestrealtype^;
  2025. end;
  2026. end;
  2027. in_pi_real :
  2028. begin
  2029. if block_type=bt_const then
  2030. setconstrealvalue(getpi)
  2031. else
  2032. resultdef:=pbestrealtype^;
  2033. end;
  2034. in_cos_real :
  2035. begin
  2036. if left.nodetype in [ordconstn,realconstn] then
  2037. setconstrealvalue(cos(getconstrealvalue))
  2038. else
  2039. begin
  2040. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2041. inserttypeconv(left,pbestrealtype^);
  2042. resultdef:=pbestrealtype^;
  2043. end;
  2044. end;
  2045. in_sin_real :
  2046. begin
  2047. if left.nodetype in [ordconstn,realconstn] then
  2048. setconstrealvalue(sin(getconstrealvalue))
  2049. else
  2050. begin
  2051. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2052. inserttypeconv(left,pbestrealtype^);
  2053. resultdef:=pbestrealtype^;
  2054. end;
  2055. end;
  2056. in_arctan_real :
  2057. begin
  2058. if left.nodetype in [ordconstn,realconstn] then
  2059. setconstrealvalue(arctan(getconstrealvalue))
  2060. else
  2061. begin
  2062. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2063. inserttypeconv(left,pbestrealtype^);
  2064. resultdef:=pbestrealtype^;
  2065. end;
  2066. end;
  2067. in_abs_real :
  2068. begin
  2069. if left.nodetype in [ordconstn,realconstn] then
  2070. setconstrealvalue(abs(getconstrealvalue))
  2071. else
  2072. begin
  2073. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2074. inserttypeconv(left,pbestrealtype^);
  2075. resultdef:=pbestrealtype^;
  2076. end;
  2077. end;
  2078. in_sqr_real :
  2079. begin
  2080. if left.nodetype in [ordconstn,realconstn] then
  2081. setconstrealvalue(sqr(getconstrealvalue))
  2082. else
  2083. begin
  2084. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2085. setfloatresultdef;
  2086. end;
  2087. end;
  2088. in_sqrt_real :
  2089. begin
  2090. if left.nodetype in [ordconstn,realconstn] then
  2091. begin
  2092. vr:=getconstrealvalue;
  2093. if vr<0.0 then
  2094. result:=handle_sqrt_const(vr)
  2095. else
  2096. setconstrealvalue(sqrt(vr));
  2097. end
  2098. else
  2099. begin
  2100. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2101. setfloatresultdef;
  2102. end;
  2103. end;
  2104. in_ln_real :
  2105. begin
  2106. if left.nodetype in [ordconstn,realconstn] then
  2107. begin
  2108. vr:=getconstrealvalue;
  2109. if vr<=0.0 then
  2110. result:=handle_ln_const(vr)
  2111. else
  2112. setconstrealvalue(ln(vr));
  2113. end
  2114. else
  2115. begin
  2116. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2117. inserttypeconv(left,pbestrealtype^);
  2118. resultdef:=pbestrealtype^;
  2119. end;
  2120. end;
  2121. {$ifdef SUPPORT_MMX}
  2122. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2123. begin
  2124. end;
  2125. {$endif SUPPORT_MMX}
  2126. in_prefetch_var:
  2127. begin
  2128. resultdef:=voidtype;
  2129. end;
  2130. {$ifdef SUPPORT_UNALIGNED}
  2131. in_unaligned_x:
  2132. begin
  2133. resultdef:=left.resultdef;
  2134. end;
  2135. {$endif SUPPORT_UNALIGNED}
  2136. in_assert_x_y :
  2137. begin
  2138. resultdef:=voidtype;
  2139. if assigned(left) then
  2140. begin
  2141. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2142. { check type }
  2143. if is_boolean(left.resultdef) then
  2144. begin
  2145. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2146. { must always be a string }
  2147. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2148. end
  2149. else
  2150. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2151. end
  2152. else
  2153. CGMessage(type_e_mismatch);
  2154. { We've checked the whole statement for correctness, now we
  2155. can remove it if assertions are off }
  2156. if not(cs_do_assertion in current_settings.localswitches) then
  2157. begin
  2158. { we need a valid node, so insert a nothingn }
  2159. result:=cnothingnode.create;
  2160. end
  2161. else
  2162. include(current_procinfo.flags,pi_do_call);
  2163. end;
  2164. in_get_frame,
  2165. in_get_caller_frame,
  2166. in_get_caller_addr:
  2167. begin
  2168. resultdef:=voidpointertype;
  2169. end;
  2170. else
  2171. internalerror(8);
  2172. end;
  2173. end;
  2174. myexit:
  2175. { Run get_paratype again to update maybe inserted typeconvs }
  2176. if not codegenerror then
  2177. begin
  2178. if assigned(left) and
  2179. (left.nodetype=callparan) then
  2180. tcallparanode(left).get_paratype;
  2181. end;
  2182. dec(parsing_para_level);
  2183. end;
  2184. function tinlinenode.pass_1 : tnode;
  2185. var
  2186. hp,hpp,resultnode : tnode;
  2187. shiftconst: longint;
  2188. tempnode: ttempcreatenode;
  2189. newstatement: tstatementnode;
  2190. newblock: tblocknode;
  2191. begin
  2192. result:=nil;
  2193. { if we handle writeln; left contains no valid address }
  2194. if assigned(left) then
  2195. begin
  2196. if left.nodetype=callparan then
  2197. tcallparanode(left).firstcallparan
  2198. else
  2199. firstpass(left);
  2200. left_max;
  2201. end;
  2202. inc(parsing_para_level);
  2203. { intern const should already be handled }
  2204. if nf_inlineconst in flags then
  2205. internalerror(200104044);
  2206. case inlinenumber of
  2207. in_lo_qword,
  2208. in_hi_qword,
  2209. in_lo_long,
  2210. in_hi_long,
  2211. in_lo_word,
  2212. in_hi_word:
  2213. begin
  2214. shiftconst := 0;
  2215. case inlinenumber of
  2216. in_hi_qword:
  2217. shiftconst := 32;
  2218. in_hi_long:
  2219. shiftconst := 16;
  2220. in_hi_word:
  2221. shiftconst := 8;
  2222. end;
  2223. if shiftconst <> 0 then
  2224. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2225. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2226. else
  2227. result := ctypeconvnode.create_internal(left,resultdef);
  2228. left := nil;
  2229. firstpass(result);
  2230. end;
  2231. in_sizeof_x:
  2232. begin
  2233. if registersint<1 then
  2234. registersint:=1;
  2235. expectloc:=LOC_REGISTER;
  2236. end;
  2237. in_typeof_x:
  2238. begin
  2239. if registersint<1 then
  2240. registersint:=1;
  2241. expectloc:=LOC_REGISTER;
  2242. end;
  2243. in_length_x:
  2244. begin
  2245. if is_shortstring(left.resultdef) then
  2246. expectloc:=left.expectloc
  2247. else
  2248. begin
  2249. { ansi/wide string }
  2250. if registersint<1 then
  2251. registersint:=1;
  2252. expectloc:=LOC_REGISTER;
  2253. end;
  2254. end;
  2255. in_typeinfo_x:
  2256. begin
  2257. expectloc:=LOC_REGISTER;
  2258. registersint:=1;
  2259. end;
  2260. in_assigned_x:
  2261. begin
  2262. expectloc := LOC_JUMP;
  2263. registersint:=1;
  2264. end;
  2265. in_pred_x,
  2266. in_succ_x:
  2267. begin
  2268. if is_64bit(resultdef) then
  2269. begin
  2270. if (registersint<2) then
  2271. registersint:=2
  2272. end
  2273. else
  2274. begin
  2275. if (registersint<1) then
  2276. registersint:=1;
  2277. end;
  2278. expectloc:=LOC_REGISTER;
  2279. end;
  2280. in_setlength_x,
  2281. in_initialize_x,
  2282. in_finalize_x:
  2283. begin
  2284. expectloc:=LOC_VOID;
  2285. end;
  2286. in_inc_x,
  2287. in_dec_x:
  2288. begin
  2289. expectloc:=LOC_VOID;
  2290. { range/overflow checking doesn't work properly }
  2291. { with the inc/dec code that's generated (JM) }
  2292. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2293. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2294. trigger an overflow. For uint32 it works because then the operation is done
  2295. in 64bit. Range checking is not applicable to pointers either }
  2296. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2297. { convert to simple add (JM) }
  2298. begin
  2299. newblock := internalstatements(newstatement);
  2300. { extra parameter? }
  2301. if assigned(tcallparanode(left).right) then
  2302. begin
  2303. { Yes, use for add node }
  2304. hpp := tcallparanode(tcallparanode(left).right).left;
  2305. tcallparanode(tcallparanode(left).right).left := nil;
  2306. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2307. CGMessage(parser_e_illegal_expression);
  2308. end
  2309. else
  2310. begin
  2311. { no, create constant 1 }
  2312. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2313. end;
  2314. typecheckpass(hpp);
  2315. if not((hpp.resultdef.typ=orddef) and
  2316. {$ifndef cpu64bit}
  2317. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2318. {$else not cpu64bit}
  2319. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2320. {$endif not cpu64bit}
  2321. inserttypeconv_internal(hpp,sinttype);
  2322. { make sure we don't call functions part of the left node twice (and generally }
  2323. { optimize the code generation) }
  2324. if node_complexity(tcallparanode(left).left) > 1 then
  2325. begin
  2326. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2327. addstatement(newstatement,tempnode);
  2328. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2329. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2330. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2331. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2332. end
  2333. else
  2334. begin
  2335. hp := tcallparanode(left).left.getcopy;
  2336. tempnode := nil;
  2337. end;
  2338. resultnode := hp.getcopy;
  2339. { avoid type errors from the addn/subn }
  2340. if not is_integer(resultnode.resultdef) then
  2341. begin
  2342. inserttypeconv_internal(hp,sinttype);
  2343. inserttypeconv_internal(hpp,sinttype);
  2344. end;
  2345. { addition/substraction depending on inc/dec }
  2346. if inlinenumber = in_inc_x then
  2347. hpp := caddnode.create(addn,hp,hpp)
  2348. else
  2349. hpp := caddnode.create(subn,hp,hpp);
  2350. { assign result of addition }
  2351. if not(is_integer(resultnode.resultdef)) then
  2352. inserttypeconv(hpp,torddef.create(
  2353. {$ifdef cpu64bit}
  2354. s64bit,
  2355. {$else cpu64bit}
  2356. s32bit,
  2357. {$endif cpu64bit}
  2358. get_min_value(resultnode.resultdef),
  2359. get_max_value(resultnode.resultdef)))
  2360. else
  2361. inserttypeconv(hpp,resultnode.resultdef);
  2362. { avoid any possible warnings }
  2363. inserttypeconv_internal(hpp,resultnode.resultdef);
  2364. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2365. { deallocate the temp }
  2366. if assigned(tempnode) then
  2367. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2368. { firstpass it }
  2369. firstpass(newblock);
  2370. { return new node }
  2371. result := newblock;
  2372. end
  2373. else if (left.resultdef.typ in [enumdef,pointerdef]) or
  2374. is_ordinal(left.resultdef) then
  2375. begin
  2376. { two paras ? }
  2377. if assigned(tcallparanode(left).right) then
  2378. begin
  2379. { need we an additional register ? }
  2380. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2381. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2382. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2383. inc(registersint);
  2384. { do we need an additional register to restore the first parameter? }
  2385. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2386. inc(registersint);
  2387. end;
  2388. end;
  2389. end;
  2390. in_include_x_y,
  2391. in_exclude_x_y:
  2392. begin
  2393. expectloc:=LOC_VOID;
  2394. registersint:=left.registersint;
  2395. registersfpu:=left.registersfpu;
  2396. {$ifdef SUPPORT_MMX}
  2397. registersmmx:=left.registersmmx;
  2398. {$endif SUPPORT_MMX}
  2399. end;
  2400. in_pack_x_y_z,
  2401. in_unpack_x_y_z:
  2402. begin
  2403. result:=first_pack_unpack;
  2404. end;
  2405. in_exp_real:
  2406. begin
  2407. result:= first_exp_real;
  2408. end;
  2409. in_round_real:
  2410. begin
  2411. result:= first_round_real;
  2412. end;
  2413. in_trunc_real:
  2414. begin
  2415. result:= first_trunc_real;
  2416. end;
  2417. in_int_real:
  2418. begin
  2419. result:= first_int_real;
  2420. end;
  2421. in_frac_real:
  2422. begin
  2423. result:= first_frac_real;
  2424. end;
  2425. in_cos_real:
  2426. begin
  2427. result:= first_cos_real;
  2428. end;
  2429. in_sin_real:
  2430. begin
  2431. result := first_sin_real;
  2432. end;
  2433. in_arctan_real:
  2434. begin
  2435. result := first_arctan_real;
  2436. end;
  2437. in_pi_real :
  2438. begin
  2439. result := first_pi;
  2440. end;
  2441. in_abs_real:
  2442. begin
  2443. result := first_abs_real;
  2444. end;
  2445. in_sqr_real:
  2446. begin
  2447. result := first_sqr_real;
  2448. end;
  2449. in_sqrt_real:
  2450. begin
  2451. result := first_sqrt_real;
  2452. end;
  2453. in_ln_real:
  2454. begin
  2455. result := first_ln_real;
  2456. end;
  2457. {$ifdef SUPPORT_MMX}
  2458. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2459. begin
  2460. end;
  2461. {$endif SUPPORT_MMX}
  2462. in_assert_x_y :
  2463. begin
  2464. expectloc:=LOC_VOID;
  2465. registersint:=left.registersint;
  2466. registersfpu:=left.registersfpu;
  2467. {$ifdef SUPPORT_MMX}
  2468. registersmmx:=left.registersmmx;
  2469. {$endif SUPPORT_MMX}
  2470. end;
  2471. in_low_x,
  2472. in_high_x:
  2473. internalerror(200104047);
  2474. in_slice_x:
  2475. internalerror(2005101501);
  2476. in_ord_x,
  2477. in_chr_byte:
  2478. begin
  2479. { should not happend as it's converted to typeconv }
  2480. internalerror(200104045);
  2481. end;
  2482. in_ofs_x :
  2483. internalerror(2000101001);
  2484. in_seg_x :
  2485. internalerror(200104046);
  2486. in_settextbuf_file_x,
  2487. in_reset_typedfile,
  2488. in_rewrite_typedfile,
  2489. in_str_x_string,
  2490. in_val_x,
  2491. in_read_x,
  2492. in_readln_x,
  2493. in_write_x,
  2494. in_writeln_x :
  2495. begin
  2496. { should be handled by pass_typecheck }
  2497. internalerror(200108234);
  2498. end;
  2499. in_get_frame:
  2500. begin
  2501. include(current_procinfo.flags,pi_needs_stackframe);
  2502. expectloc:=LOC_CREGISTER;
  2503. end;
  2504. in_get_caller_frame:
  2505. begin
  2506. expectloc:=LOC_REGISTER;
  2507. registersint:=1;
  2508. end;
  2509. in_get_caller_addr:
  2510. begin
  2511. expectloc:=LOC_REGISTER;
  2512. registersint:=1;
  2513. end;
  2514. in_prefetch_var:
  2515. begin
  2516. expectloc:=LOC_VOID;
  2517. end;
  2518. {$ifdef SUPPORT_UNALIGNED}
  2519. in_unaligned_x:
  2520. begin
  2521. expectloc:=tcallparanode(left).left.expectloc;
  2522. end;
  2523. {$endif SUPPORT_UNALIGNED}
  2524. else
  2525. internalerror(89);
  2526. end;
  2527. dec(parsing_para_level);
  2528. end;
  2529. {$maxfpuregisters default}
  2530. function tinlinenode.docompare(p: tnode): boolean;
  2531. begin
  2532. docompare :=
  2533. inherited docompare(p) and
  2534. (inlinenumber = tinlinenode(p).inlinenumber);
  2535. end;
  2536. function tinlinenode.first_pi : tnode;
  2537. begin
  2538. result:=crealconstnode.create(getpi,pbestrealtype^);
  2539. end;
  2540. function tinlinenode.first_arctan_real : tnode;
  2541. begin
  2542. { create the call to the helper }
  2543. { on entry left node contains the parameter }
  2544. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2545. ccallparanode.create(left,nil));
  2546. left := nil;
  2547. end;
  2548. function tinlinenode.first_abs_real : tnode;
  2549. begin
  2550. { create the call to the helper }
  2551. { on entry left node contains the parameter }
  2552. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2553. ccallparanode.create(left,nil));
  2554. left := nil;
  2555. end;
  2556. function tinlinenode.first_sqr_real : tnode;
  2557. begin
  2558. { create the call to the helper }
  2559. { on entry left node contains the parameter }
  2560. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2561. ccallparanode.create(left,nil)),resultdef);
  2562. left := nil;
  2563. end;
  2564. function tinlinenode.first_sqrt_real : tnode;
  2565. begin
  2566. { create the call to the helper }
  2567. { on entry left node contains the parameter }
  2568. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2569. ccallparanode.create(left,nil)),resultdef);
  2570. left := nil;
  2571. end;
  2572. function tinlinenode.first_ln_real : tnode;
  2573. begin
  2574. { create the call to the helper }
  2575. { on entry left node contains the parameter }
  2576. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2577. ccallparanode.create(left,nil));
  2578. left := nil;
  2579. end;
  2580. function tinlinenode.first_cos_real : tnode;
  2581. begin
  2582. { create the call to the helper }
  2583. { on entry left node contains the parameter }
  2584. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2585. ccallparanode.create(left,nil));
  2586. left := nil;
  2587. end;
  2588. function tinlinenode.first_sin_real : tnode;
  2589. begin
  2590. { create the call to the helper }
  2591. { on entry left node contains the parameter }
  2592. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2593. ccallparanode.create(left,nil));
  2594. left := nil;
  2595. end;
  2596. function tinlinenode.first_exp_real : tnode;
  2597. begin
  2598. { create the call to the helper }
  2599. { on entry left node contains the parameter }
  2600. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2601. left := nil;
  2602. end;
  2603. function tinlinenode.first_int_real : tnode;
  2604. begin
  2605. { create the call to the helper }
  2606. { on entry left node contains the parameter }
  2607. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2608. left := nil;
  2609. end;
  2610. function tinlinenode.first_frac_real : tnode;
  2611. begin
  2612. { create the call to the helper }
  2613. { on entry left node contains the parameter }
  2614. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2615. left := nil;
  2616. end;
  2617. function tinlinenode.first_round_real : tnode;
  2618. begin
  2619. { create the call to the helper }
  2620. { on entry left node contains the parameter }
  2621. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2622. left := nil;
  2623. end;
  2624. function tinlinenode.first_trunc_real : tnode;
  2625. begin
  2626. { create the call to the helper }
  2627. { on entry left node contains the parameter }
  2628. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2629. left := nil;
  2630. end;
  2631. function tinlinenode.first_pack_unpack: tnode;
  2632. var
  2633. loopstatement : tstatementnode;
  2634. loop : tblocknode;
  2635. loopvar : ttempcreatenode;
  2636. tempnode,
  2637. source,
  2638. target,
  2639. index,
  2640. unpackednode,
  2641. packednode,
  2642. sourcevecindex,
  2643. targetvecindex,
  2644. loopbody : tnode;
  2645. temprangedef : tdef;
  2646. ulorange,
  2647. uhirange,
  2648. plorange,
  2649. phirange : TConstExprInt;
  2650. begin
  2651. { transform into a for loop which assigns the data of the (un)packed }
  2652. { array to the other one }
  2653. source := left;
  2654. if (inlinenumber = in_unpack_x_y_z) then
  2655. begin
  2656. target := tcallparanode(source).right;
  2657. index := tcallparanode(target).right;
  2658. packednode := tcallparanode(source).left;
  2659. unpackednode := tcallparanode(target).left;
  2660. end
  2661. else
  2662. begin
  2663. index := tcallparanode(source).right;
  2664. target := tcallparanode(index).right;
  2665. packednode := tcallparanode(target).left;
  2666. unpackednode := tcallparanode(source).left;
  2667. end;
  2668. source := tcallparanode(source).left;
  2669. target := tcallparanode(target).left;
  2670. index := tcallparanode(index).left;
  2671. loop := internalstatements(loopstatement);
  2672. loopvar := ctempcreatenode.create(
  2673. tarraydef(packednode.resultdef).rangedef,
  2674. tarraydef(packednode.resultdef).rangedef.size,
  2675. tt_persistent,true);
  2676. addstatement(loopstatement,loopvar);
  2677. { For range checking: we have to convert to an integer type (in case the index type }
  2678. { is an enum), add the index and loop variable together, convert the result }
  2679. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2680. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2681. { errors }
  2682. temprangedef:=nil;
  2683. getrange(unpackednode.resultdef,ulorange,uhirange);
  2684. getrange(packednode.resultdef,plorange,phirange);
  2685. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2686. sourcevecindex := ctemprefnode.create(loopvar);
  2687. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2688. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2689. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2690. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2691. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2692. if (inlinenumber = in_pack_x_y_z) then
  2693. begin
  2694. { swap source and target vec indices }
  2695. tempnode := sourcevecindex;
  2696. sourcevecindex := targetvecindex;
  2697. targetvecindex := tempnode;
  2698. end;
  2699. { create the assignment in the loop body }
  2700. loopbody :=
  2701. cassignmentnode.create(
  2702. cvecnode.create(target.getcopy,targetvecindex),
  2703. cvecnode.create(source.getcopy,sourcevecindex)
  2704. );
  2705. { create the actual for loop }
  2706. tempnode := cfornode.create(
  2707. ctemprefnode.create(loopvar),
  2708. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2709. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2710. loopbody,
  2711. false);
  2712. addstatement(loopstatement,tempnode);
  2713. { free the loop counter }
  2714. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2715. result := loop;
  2716. end;
  2717. begin
  2718. cinlinenode:=tinlinenode;
  2719. end.