ninl.pas 119 KB

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