ninl.pas 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  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. checkrange: boolean;
  1271. begin { simplify }
  1272. result:=nil;
  1273. { handle intern constant functions in separate case }
  1274. if nf_inlineconst in flags then
  1275. begin
  1276. { no parameters? }
  1277. if not assigned(left) then
  1278. internalerror(200501231)
  1279. else
  1280. begin
  1281. vl:=0;
  1282. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1283. case left.nodetype of
  1284. realconstn :
  1285. begin
  1286. { Real functions are all handled with internproc below }
  1287. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1288. end;
  1289. ordconstn :
  1290. vl:=tordconstnode(left).value;
  1291. callparan :
  1292. begin
  1293. { both exists, else it was not generated }
  1294. vl:=tordconstnode(tcallparanode(left).left).value;
  1295. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1296. end;
  1297. else
  1298. CGMessage(parser_e_illegal_expression);
  1299. end;
  1300. case inlinenumber of
  1301. in_const_abs :
  1302. if vl.signed then
  1303. hp:=genintconstnode(abs(vl.svalue))
  1304. else
  1305. hp:=genintconstnode(vl.uvalue);
  1306. in_const_sqr:
  1307. if vl.signed then
  1308. hp:=genintconstnode(sqr(vl.svalue))
  1309. else
  1310. hp:=genintconstnode(sqr(vl.uvalue));
  1311. in_const_odd :
  1312. hp:=cordconstnode.create(qword(odd(int64(vl))),booltype,true);
  1313. in_const_swap_word :
  1314. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1315. in_const_swap_long :
  1316. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1317. in_const_swap_qword :
  1318. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1319. in_const_ptr:
  1320. begin
  1321. {Don't construct pointers from negative values.}
  1322. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1323. cgmessage(parser_e_range_check_error);
  1324. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1325. end
  1326. else
  1327. internalerror(88);
  1328. end;
  1329. end;
  1330. if hp=nil then
  1331. hp:=cerrornode.create;
  1332. result:=hp;
  1333. end
  1334. else
  1335. begin
  1336. case inlinenumber of
  1337. in_lo_long,
  1338. in_hi_long,
  1339. in_lo_qword,
  1340. in_hi_qword,
  1341. in_lo_word,
  1342. in_hi_word :
  1343. begin
  1344. if left.nodetype=ordconstn then
  1345. begin
  1346. case inlinenumber of
  1347. in_lo_word :
  1348. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1349. in_hi_word :
  1350. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1351. in_lo_long :
  1352. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1353. in_hi_long :
  1354. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1355. in_lo_qword :
  1356. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1357. in_hi_qword :
  1358. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1359. end;
  1360. end;
  1361. end;
  1362. in_ord_x:
  1363. begin
  1364. case left.resultdef.typ of
  1365. orddef :
  1366. begin
  1367. case torddef(left.resultdef).ordtype of
  1368. pasbool,
  1369. uchar:
  1370. begin
  1371. { change to byte() }
  1372. result:=ctypeconvnode.create_internal(left,u8inttype);
  1373. left:=nil;
  1374. end;
  1375. uwidechar :
  1376. begin
  1377. { change to word() }
  1378. result:=ctypeconvnode.create_internal(left,u16inttype);
  1379. left:=nil;
  1380. end;
  1381. bool8bit:
  1382. begin
  1383. { change to shortint() }
  1384. result:=ctypeconvnode.create_internal(left,s8inttype);
  1385. left:=nil;
  1386. end;
  1387. bool16bit :
  1388. begin
  1389. { change to smallint() }
  1390. result:=ctypeconvnode.create_internal(left,s16inttype);
  1391. left:=nil;
  1392. end;
  1393. bool32bit :
  1394. begin
  1395. { change to longint() }
  1396. result:=ctypeconvnode.create_internal(left,s32inttype);
  1397. left:=nil;
  1398. end;
  1399. bool64bit :
  1400. begin
  1401. { change to int64() }
  1402. result:=ctypeconvnode.create_internal(left,s64inttype);
  1403. left:=nil;
  1404. end;
  1405. uvoid :
  1406. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1407. else
  1408. begin
  1409. { all other orddef need no transformation }
  1410. result:=left;
  1411. left:=nil;
  1412. end;
  1413. end;
  1414. end;
  1415. enumdef :
  1416. begin
  1417. result:=ctypeconvnode.create_internal(left,s32inttype);
  1418. left:=nil;
  1419. end;
  1420. pointerdef :
  1421. begin
  1422. if m_mac in current_settings.modeswitches then
  1423. begin
  1424. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1425. left:=nil;
  1426. end
  1427. end;
  1428. end;
  1429. (*
  1430. if (left.nodetype=ordconstn) then
  1431. begin
  1432. result:=cordconstnode.create(
  1433. tordconstnode(left).value,sinttype,true);
  1434. end
  1435. else if (m_mac in current_settings.modeswitches) and
  1436. (left.ndoetype=pointerconstn) then
  1437. result:=cordconstnode.create(
  1438. tpointerconstnode(left).value,ptruinttype,true);
  1439. *)
  1440. end;
  1441. in_chr_byte:
  1442. begin
  1443. { convert to explicit char() }
  1444. result:=ctypeconvnode.create_internal(left,cchartype);
  1445. left:=nil;
  1446. end;
  1447. in_length_x:
  1448. begin
  1449. case left.resultdef.typ of
  1450. stringdef :
  1451. begin
  1452. if (left.nodetype=stringconstn) then
  1453. begin
  1454. result:=cordconstnode.create(
  1455. tstringconstnode(left).len,sinttype,true);
  1456. end;
  1457. end;
  1458. orddef :
  1459. begin
  1460. { length of char is always one }
  1461. if is_char(left.resultdef) or
  1462. is_widechar(left.resultdef) then
  1463. begin
  1464. result:=cordconstnode.create(1,sinttype,false);
  1465. end
  1466. end;
  1467. arraydef :
  1468. begin
  1469. if not is_open_array(left.resultdef) and
  1470. not is_array_of_const(left.resultdef) and
  1471. not is_dynamic_array(left.resultdef) then
  1472. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1473. tarraydef(left.resultdef).lowrange+1,
  1474. sinttype,true);
  1475. end;
  1476. end;
  1477. end;
  1478. in_assigned_x:
  1479. begin
  1480. if is_constnode(tcallparanode(left).left) or
  1481. (tcallparanode(left).left.nodetype = pointerconstn) then
  1482. begin
  1483. { let an add node figure it out }
  1484. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1485. tcallparanode(left).left := nil;
  1486. end;
  1487. end;
  1488. in_pred_x,
  1489. in_succ_x:
  1490. begin
  1491. { only perform range checking if the result is an enum }
  1492. checkrange:=(resultdef.typ=enumdef);
  1493. if (left.nodetype=ordconstn) then
  1494. begin
  1495. if (inlinenumber=in_succ_x) then
  1496. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1497. else
  1498. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1499. end;
  1500. end;
  1501. in_low_x,
  1502. in_high_x:
  1503. begin
  1504. case left.resultdef.typ of
  1505. orddef,
  1506. enumdef:
  1507. begin
  1508. result:=do_lowhigh(left.resultdef);
  1509. end;
  1510. setdef:
  1511. begin
  1512. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1513. end;
  1514. arraydef:
  1515. begin
  1516. if (inlinenumber=in_low_x) then
  1517. begin
  1518. result:=cordconstnode.create(int64(tarraydef(
  1519. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  1520. end
  1521. else if not is_open_array(left.resultdef) and
  1522. not is_array_of_const(left.resultdef) and
  1523. not is_dynamic_array(left.resultdef) then
  1524. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  1525. tarraydef(left.resultdef).rangedef,true);
  1526. end;
  1527. stringdef:
  1528. begin
  1529. if inlinenumber=in_low_x then
  1530. begin
  1531. result:=cordconstnode.create(0,u8inttype,false);
  1532. end
  1533. else if not is_ansistring(left.resultdef) and
  1534. not is_wide_or_unicode_string(left.resultdef) then
  1535. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  1536. end;
  1537. end;
  1538. end;
  1539. in_exp_real :
  1540. begin
  1541. if left.nodetype in [ordconstn,realconstn] then
  1542. begin
  1543. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1544. if (trealconstnode(result).value_real=MathInf.Value) and
  1545. floating_point_range_check_error then
  1546. begin
  1547. result:=crealconstnode.create(0,pbestrealtype^);
  1548. CGMessage(parser_e_range_check_error);
  1549. end;
  1550. end
  1551. end;
  1552. in_trunc_real :
  1553. begin
  1554. if left.nodetype in [ordconstn,realconstn] then
  1555. begin
  1556. vr:=getconstrealvalue;
  1557. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1558. begin
  1559. CGMessage(parser_e_range_check_error);
  1560. result:=cordconstnode.create(1,s64inttype,false)
  1561. end
  1562. else
  1563. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1564. end
  1565. end;
  1566. in_round_real :
  1567. begin
  1568. if left.nodetype in [ordconstn,realconstn] then
  1569. begin
  1570. vr:=getconstrealvalue;
  1571. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1572. begin
  1573. CGMessage(parser_e_range_check_error);
  1574. result:=cordconstnode.create(1,s64inttype,false)
  1575. end
  1576. else
  1577. result:=cordconstnode.create(round(vr),s64inttype,true)
  1578. end
  1579. end;
  1580. in_frac_real :
  1581. begin
  1582. if left.nodetype in [ordconstn,realconstn] then
  1583. setconstrealvalue(frac(getconstrealvalue))
  1584. end;
  1585. in_int_real :
  1586. begin
  1587. if left.nodetype in [ordconstn,realconstn] then
  1588. setconstrealvalue(int(getconstrealvalue));
  1589. end;
  1590. in_pi_real :
  1591. begin
  1592. if block_type=bt_const then
  1593. setconstrealvalue(getpi)
  1594. end;
  1595. in_cos_real :
  1596. begin
  1597. if left.nodetype in [ordconstn,realconstn] then
  1598. setconstrealvalue(cos(getconstrealvalue))
  1599. end;
  1600. in_sin_real :
  1601. begin
  1602. if left.nodetype in [ordconstn,realconstn] then
  1603. setconstrealvalue(sin(getconstrealvalue))
  1604. end;
  1605. in_arctan_real :
  1606. begin
  1607. if left.nodetype in [ordconstn,realconstn] then
  1608. setconstrealvalue(arctan(getconstrealvalue))
  1609. end;
  1610. in_abs_real :
  1611. begin
  1612. if left.nodetype in [ordconstn,realconstn] then
  1613. setconstrealvalue(abs(getconstrealvalue))
  1614. end;
  1615. in_abs_long:
  1616. begin
  1617. if left.nodetype=ordconstn then
  1618. begin
  1619. if tordconstnode(left).value<0 then
  1620. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  1621. else
  1622. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  1623. end
  1624. end;
  1625. in_sqr_real :
  1626. begin
  1627. if left.nodetype in [ordconstn,realconstn] then
  1628. setconstrealvalue(sqr(getconstrealvalue))
  1629. end;
  1630. in_sqrt_real :
  1631. begin
  1632. if left.nodetype in [ordconstn,realconstn] then
  1633. begin
  1634. vr:=getconstrealvalue;
  1635. if vr<0.0 then
  1636. result:=handle_sqrt_const(vr)
  1637. else
  1638. setconstrealvalue(sqrt(vr));
  1639. end
  1640. end;
  1641. in_ln_real :
  1642. begin
  1643. if left.nodetype in [ordconstn,realconstn] then
  1644. begin
  1645. vr:=getconstrealvalue;
  1646. if vr<=0.0 then
  1647. result:=handle_ln_const(vr)
  1648. else
  1649. setconstrealvalue(ln(vr));
  1650. end
  1651. end;
  1652. in_assert_x_y :
  1653. begin
  1654. if not(cs_do_assertion in current_settings.localswitches) then
  1655. { we need a valid node, so insert a nothingn }
  1656. result:=cnothingnode.create;
  1657. end;
  1658. end;
  1659. end;
  1660. end;
  1661. function tinlinenode.pass_typecheck:tnode;
  1662. procedure setfloatresultdef;
  1663. begin
  1664. if (left.resultdef.typ=floatdef) and
  1665. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1666. resultdef:=left.resultdef
  1667. else
  1668. begin
  1669. if (left.nodetype <> ordconstn) then
  1670. inserttypeconv(left,pbestrealtype^);
  1671. resultdef:=pbestrealtype^;
  1672. end;
  1673. end;
  1674. procedure handle_pack_unpack;
  1675. var
  1676. source, target, index: tcallparanode;
  1677. unpackedarraydef, packedarraydef: tarraydef;
  1678. tempindex: TConstExprInt;
  1679. begin
  1680. resultdef:=voidtype;
  1681. unpackedarraydef := nil;
  1682. packedarraydef := nil;
  1683. source := tcallparanode(left);
  1684. if (inlinenumber = in_unpack_x_y_z) then
  1685. begin
  1686. target := tcallparanode(source.right);
  1687. index := tcallparanode(target.right);
  1688. { source must be a packed array }
  1689. if not is_packed_array(source.left.resultdef) then
  1690. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1691. else
  1692. packedarraydef := tarraydef(source.left.resultdef);
  1693. { target can be any kind of array, as long as it's not packed }
  1694. if (target.left.resultdef.typ <> arraydef) or
  1695. is_packed_array(target.left.resultdef) then
  1696. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1697. else
  1698. unpackedarraydef := tarraydef(target.left.resultdef);
  1699. end
  1700. else
  1701. begin
  1702. index := tcallparanode(source.right);
  1703. target := tcallparanode(index.right);
  1704. { source can be any kind of array, as long as it's not packed }
  1705. if (source.left.resultdef.typ <> arraydef) or
  1706. is_packed_array(source.left.resultdef) then
  1707. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1708. else
  1709. unpackedarraydef := tarraydef(source.left.resultdef);
  1710. { target must be a packed array }
  1711. if not is_packed_array(target.left.resultdef) then
  1712. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1713. else
  1714. packedarraydef := tarraydef(target.left.resultdef);
  1715. end;
  1716. if assigned(unpackedarraydef) then
  1717. begin
  1718. { index must be compatible with the unpacked array's indextype }
  1719. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1720. { range check at compile time if possible }
  1721. if assigned(packedarraydef) and
  1722. (index.left.nodetype = ordconstn) and
  1723. not is_special_array(unpackedarraydef) then
  1724. begin
  1725. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1726. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1727. testrange(unpackedarraydef,tempindex,false);
  1728. end;
  1729. end;
  1730. { source array is read and must be valid }
  1731. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1732. { target array is written }
  1733. valid_for_assignment(target.left,true);
  1734. set_varstate(target.left,vs_written,[]);
  1735. { index in the unpacked array is read and must be valid }
  1736. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1737. { if the size of the arrays is 0 (array of empty records), }
  1738. { do nothing }
  1739. if (source.resultdef.size = 0) then
  1740. result:=cnothingnode.create;
  1741. end;
  1742. function handle_objc_encode: tnode;
  1743. var
  1744. encodedtype: ansistring;
  1745. errordef: tdef;
  1746. begin
  1747. encodedtype:='';
  1748. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  1749. Message1(type_e_objc_type_unsupported,errordef.typename);
  1750. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype));
  1751. end;
  1752. var
  1753. hightree,
  1754. hp : tnode;
  1755. begin
  1756. result:=nil;
  1757. { when handling writeln "left" contains no valid address }
  1758. if assigned(left) then
  1759. begin
  1760. if left.nodetype=callparan then
  1761. tcallparanode(left).get_paratype
  1762. else
  1763. typecheckpass(left);
  1764. end;
  1765. if not(nf_inlineconst in flags) then
  1766. begin
  1767. case inlinenumber of
  1768. in_lo_long,
  1769. in_hi_long,
  1770. in_lo_qword,
  1771. in_hi_qword,
  1772. in_lo_word,
  1773. in_hi_word :
  1774. begin
  1775. { give warning for incompatibility with tp and delphi }
  1776. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1777. ((m_tp7 in current_settings.modeswitches) or
  1778. (m_delphi in current_settings.modeswitches)) then
  1779. CGMessage(type_w_maybe_wrong_hi_lo);
  1780. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1781. if not is_integer(left.resultdef) then
  1782. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1783. case inlinenumber of
  1784. in_lo_word,
  1785. in_hi_word :
  1786. resultdef:=u8inttype;
  1787. in_lo_long,
  1788. in_hi_long :
  1789. resultdef:=u16inttype;
  1790. in_lo_qword,
  1791. in_hi_qword :
  1792. resultdef:=u32inttype;
  1793. end;
  1794. end;
  1795. in_sizeof_x:
  1796. begin
  1797. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  1798. set_varstate(left,vs_read,[]);
  1799. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1800. begin
  1801. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1802. if assigned(hightree) then
  1803. begin
  1804. hp:=caddnode.create(addn,hightree,
  1805. cordconstnode.create(1,sinttype,false));
  1806. if (left.resultdef.typ=arraydef) then
  1807. if not is_packed_array(tarraydef(left.resultdef)) then
  1808. begin
  1809. if (tarraydef(left.resultdef).elesize<>1) then
  1810. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1811. left.resultdef).elesize,sinttype,true));
  1812. end
  1813. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1814. begin
  1815. { no packed open array support yet }
  1816. if (hp.nodetype <> ordconstn) then
  1817. internalerror(2006081511);
  1818. hp.free;
  1819. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1820. {
  1821. hp:=
  1822. ctypeconvnode.create_explicit(sinttype,
  1823. cmoddivnode.create(divn,
  1824. caddnode.create(addn,
  1825. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1826. left.resultdef).elepackedbitsize,s64inttype,true)),
  1827. cordconstnode.create(a,s64inttype,true)),
  1828. cordconstnode.create(8,s64inttype,true)),
  1829. sinttype);
  1830. }
  1831. end;
  1832. result:=hp;
  1833. end;
  1834. end
  1835. else
  1836. resultdef:=sinttype;
  1837. end;
  1838. in_typeof_x:
  1839. begin
  1840. set_varstate(left,vs_read,[]);
  1841. resultdef:=voidpointertype;
  1842. end;
  1843. in_ord_x:
  1844. begin
  1845. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1846. case left.resultdef.typ of
  1847. orddef,
  1848. enumdef :
  1849. ;
  1850. pointerdef :
  1851. begin
  1852. if not(m_mac in current_settings.modeswitches) then
  1853. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1854. end
  1855. else
  1856. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1857. end;
  1858. end;
  1859. in_chr_byte:
  1860. begin
  1861. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1862. end;
  1863. in_length_x:
  1864. begin
  1865. if ((left.resultdef.typ=arraydef) and
  1866. (not is_special_array(left.resultdef) or
  1867. is_open_array(left.resultdef))) or
  1868. (left.resultdef.typ=orddef) then
  1869. set_varstate(left,vs_read,[])
  1870. else
  1871. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1872. case left.resultdef.typ of
  1873. variantdef:
  1874. begin
  1875. inserttypeconv(left,cansistringtype);
  1876. end;
  1877. stringdef :
  1878. begin
  1879. { we don't need string convertions here, }
  1880. { except if from widestring to ansistring }
  1881. { and vice versa (that can change the }
  1882. { length) }
  1883. if (left.nodetype=typeconvn) and
  1884. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  1885. not(is_wide_or_unicode_string(left.resultdef) xor
  1886. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  1887. begin
  1888. hp:=ttypeconvnode(left).left;
  1889. ttypeconvnode(left).left:=nil;
  1890. left.free;
  1891. left:=hp;
  1892. end;
  1893. end;
  1894. orddef :
  1895. begin
  1896. { will be handled in simplify }
  1897. if not is_char(left.resultdef) and
  1898. not is_widechar(left.resultdef) then
  1899. CGMessage(type_e_mismatch);
  1900. end;
  1901. pointerdef :
  1902. begin
  1903. if is_pchar(left.resultdef) then
  1904. begin
  1905. hp := ccallparanode.create(left,nil);
  1906. result := ccallnode.createintern('fpc_pchar_length',hp);
  1907. { make sure the left node doesn't get disposed, since it's }
  1908. { reused in the new node (JM) }
  1909. left:=nil;
  1910. exit;
  1911. end
  1912. else if is_pwidechar(left.resultdef) then
  1913. begin
  1914. hp := ccallparanode.create(left,nil);
  1915. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1916. { make sure the left node doesn't get disposed, since it's }
  1917. { reused in the new node (JM) }
  1918. left:=nil;
  1919. exit;
  1920. end
  1921. else
  1922. CGMessage(type_e_mismatch);
  1923. end;
  1924. arraydef :
  1925. begin
  1926. if is_open_array(left.resultdef) or
  1927. is_array_of_const(left.resultdef) then
  1928. begin
  1929. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1930. if assigned(hightree) then
  1931. result:=caddnode.create(addn,hightree,
  1932. cordconstnode.create(1,sinttype,false));
  1933. exit;
  1934. end
  1935. else if is_dynamic_array(left.resultdef) then
  1936. begin
  1937. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1938. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1939. { make sure the left node doesn't get disposed, since it's }
  1940. { reused in the new node (JM) }
  1941. left:=nil;
  1942. exit;
  1943. end
  1944. else
  1945. begin
  1946. { will be handled in simplify }
  1947. end;
  1948. end
  1949. else
  1950. CGMessage(type_e_mismatch);
  1951. end;
  1952. { shortstring return an 8 bit value as the length
  1953. is the first byte of the string }
  1954. if is_shortstring(left.resultdef) then
  1955. resultdef:=u8inttype
  1956. else
  1957. resultdef:=sinttype;
  1958. end;
  1959. in_typeinfo_x:
  1960. begin
  1961. if (left.resultdef.typ=enumdef) and
  1962. (tenumdef(left.resultdef).has_jumps) then
  1963. CGMessage(type_e_no_type_info);
  1964. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1965. resultdef:=voidpointertype;
  1966. end;
  1967. in_assigned_x:
  1968. begin
  1969. { the parser has already made sure the expression is valid }
  1970. { there could be a procvar, which is 2*sizeof(pointer), while we }
  1971. { must only check the first pointer -> can't just convert to an }
  1972. { add node in all cases }
  1973. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1974. resultdef:=booltype;
  1975. end;
  1976. in_ofs_x :
  1977. internalerror(2000101001);
  1978. in_seg_x :
  1979. begin
  1980. set_varstate(left,vs_read,[]);
  1981. result:=cordconstnode.create(0,s32inttype,false);
  1982. end;
  1983. in_pred_x,
  1984. in_succ_x:
  1985. begin
  1986. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1987. resultdef:=left.resultdef;
  1988. if not is_ordinal(resultdef) then
  1989. CGMessage(type_e_ordinal_expr_expected)
  1990. else
  1991. begin
  1992. if (resultdef.typ=enumdef) and
  1993. (tenumdef(resultdef).has_jumps) and
  1994. not(m_delphi in current_settings.modeswitches) then
  1995. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1996. end;
  1997. end;
  1998. in_initialize_x,
  1999. in_finalize_x,
  2000. in_setlength_x:
  2001. begin
  2002. { inlined from pinline }
  2003. internalerror(200204231);
  2004. end;
  2005. in_inc_x,
  2006. in_dec_x:
  2007. begin
  2008. resultdef:=voidtype;
  2009. if assigned(left) then
  2010. begin
  2011. { first param must be var }
  2012. valid_for_var(tcallparanode(left).left,true);
  2013. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2014. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2015. is_ordinal(left.resultdef) or
  2016. is_currency(left.resultdef) then
  2017. begin
  2018. { value of left gets changed -> must be unique }
  2019. set_unique(tcallparanode(left).left);
  2020. { two paras ? }
  2021. if assigned(tcallparanode(left).right) then
  2022. begin
  2023. if is_integer(tcallparanode(left).right.resultdef) then
  2024. begin
  2025. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2026. { when range/overflow checking is on, we
  2027. convert this to a regular add, and for proper
  2028. checking we need the original type }
  2029. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2030. if is_integer(tcallparanode(left).left.resultdef) then
  2031. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2032. else
  2033. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2034. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2035. { should be handled in the parser (JM) }
  2036. internalerror(2006020901);
  2037. end
  2038. else
  2039. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2040. end;
  2041. end
  2042. else
  2043. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2044. end
  2045. else
  2046. CGMessagePos(fileinfo,type_e_mismatch);
  2047. end;
  2048. in_read_x,
  2049. in_readln_x,
  2050. in_readstr_x,
  2051. in_write_x,
  2052. in_writeln_x,
  2053. in_writestr_x :
  2054. begin
  2055. result := handle_read_write;
  2056. end;
  2057. in_settextbuf_file_x :
  2058. begin
  2059. resultdef:=voidtype;
  2060. { now we know the type of buffer }
  2061. hp:=ccallparanode.create(cordconstnode.create(
  2062. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2063. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2064. left:=nil;
  2065. end;
  2066. { the firstpass of the arg has been done in firstcalln ? }
  2067. in_reset_typedfile,
  2068. in_rewrite_typedfile :
  2069. begin
  2070. result := handle_reset_rewrite_typed;
  2071. end;
  2072. in_str_x_string :
  2073. begin
  2074. result:=handle_str;
  2075. end;
  2076. in_val_x :
  2077. begin
  2078. result:=handle_val;
  2079. end;
  2080. in_include_x_y,
  2081. in_exclude_x_y:
  2082. begin
  2083. resultdef:=voidtype;
  2084. { the parser already checks whether we have two (and exactly two) }
  2085. { parameters (JM) }
  2086. { first param must be var }
  2087. valid_for_var(tcallparanode(left).left,true);
  2088. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2089. { check type }
  2090. if (left.resultdef.typ=setdef) then
  2091. begin
  2092. { insert a type conversion }
  2093. { to the type of the set elements }
  2094. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2095. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2096. tsetdef(left.resultdef).elementdef);
  2097. end
  2098. else
  2099. CGMessage(type_e_mismatch);
  2100. end;
  2101. in_pack_x_y_z,
  2102. in_unpack_x_y_z :
  2103. begin
  2104. handle_pack_unpack;
  2105. end;
  2106. in_slice_x:
  2107. begin
  2108. result:=nil;
  2109. resultdef:=tcallparanode(left).left.resultdef;
  2110. if (resultdef.typ <> arraydef) then
  2111. CGMessagePos(left.fileinfo,type_e_mismatch)
  2112. else if is_packed_array(resultdef) then
  2113. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2114. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2115. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2116. type_e_integer_expr_expected,
  2117. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2118. end;
  2119. in_low_x,
  2120. in_high_x:
  2121. begin
  2122. case left.resultdef.typ of
  2123. orddef,
  2124. enumdef,
  2125. setdef:
  2126. ;
  2127. arraydef:
  2128. begin
  2129. if (inlinenumber=in_low_x) then
  2130. set_varstate(left,vs_read,[])
  2131. else
  2132. begin
  2133. if is_open_array(left.resultdef) or
  2134. is_array_of_const(left.resultdef) then
  2135. begin
  2136. set_varstate(left,vs_read,[]);
  2137. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2138. end
  2139. else
  2140. if is_dynamic_array(left.resultdef) then
  2141. begin
  2142. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2143. { can't use inserttypeconv because we need }
  2144. { an explicit type conversion (JM) }
  2145. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2146. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2147. { make sure the left node doesn't get disposed, since it's }
  2148. { reused in the new node (JM) }
  2149. left:=nil;
  2150. end
  2151. else
  2152. begin
  2153. set_varstate(left,vs_read,[]);
  2154. end;
  2155. end;
  2156. end;
  2157. stringdef:
  2158. begin
  2159. if inlinenumber=in_low_x then
  2160. begin
  2161. set_varstate(left,vs_read,[]);
  2162. end
  2163. else
  2164. begin
  2165. if is_open_string(left.resultdef) then
  2166. begin
  2167. set_varstate(left,vs_read,[]);
  2168. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2169. end
  2170. else if is_ansistring(left.resultdef) or
  2171. is_wide_or_unicode_string(left.resultdef) then
  2172. CGMessage(type_e_mismatch)
  2173. end;
  2174. end;
  2175. else
  2176. CGMessage(type_e_mismatch);
  2177. end;
  2178. end;
  2179. in_exp_real,
  2180. in_frac_real,
  2181. in_int_real,
  2182. in_cos_real,
  2183. in_sin_real,
  2184. in_arctan_real,
  2185. in_abs_real,
  2186. in_ln_real :
  2187. begin
  2188. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2189. { converting an int64 to double on platforms without }
  2190. { extended can cause precision loss }
  2191. if not(left.nodetype in [ordconstn,realconstn]) then
  2192. inserttypeconv(left,pbestrealtype^);
  2193. resultdef:=pbestrealtype^;
  2194. end;
  2195. in_trunc_real,
  2196. in_round_real :
  2197. begin
  2198. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2199. { for direct float rounding, no best real type cast should be necessary }
  2200. if not((left.resultdef.typ=floatdef) and
  2201. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real])) and
  2202. { converting an int64 to double on platforms without }
  2203. { extended can cause precision loss }
  2204. not(left.nodetype in [ordconstn,realconstn]) then
  2205. inserttypeconv(left,pbestrealtype^);
  2206. resultdef:=s64inttype;
  2207. end;
  2208. in_pi_real :
  2209. begin
  2210. resultdef:=pbestrealtype^;
  2211. end;
  2212. in_abs_long:
  2213. begin
  2214. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2215. inserttypeconv(left,s32inttype);
  2216. resultdef:=s32inttype;
  2217. end;
  2218. in_sqr_real,
  2219. in_sqrt_real :
  2220. begin
  2221. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2222. setfloatresultdef;
  2223. end;
  2224. {$ifdef SUPPORT_MMX}
  2225. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2226. begin
  2227. end;
  2228. {$endif SUPPORT_MMX}
  2229. in_unaligned_x:
  2230. begin
  2231. resultdef:=left.resultdef;
  2232. end;
  2233. in_assert_x_y :
  2234. begin
  2235. resultdef:=voidtype;
  2236. if assigned(left) then
  2237. begin
  2238. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2239. { check type }
  2240. if is_boolean(left.resultdef) then
  2241. begin
  2242. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2243. { must always be a string }
  2244. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2245. end
  2246. else
  2247. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2248. end
  2249. else
  2250. CGMessage(type_e_mismatch);
  2251. if (cs_do_assertion in current_settings.localswitches) then
  2252. include(current_procinfo.flags,pi_do_call);
  2253. end;
  2254. in_prefetch_var:
  2255. resultdef:=voidtype;
  2256. in_get_frame,
  2257. in_get_caller_frame,
  2258. in_get_caller_addr:
  2259. begin
  2260. resultdef:=voidpointertype;
  2261. end;
  2262. in_rol_x,
  2263. in_ror_x:
  2264. begin
  2265. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2266. resultdef:=left.resultdef;
  2267. end;
  2268. in_rol_x_x,
  2269. in_ror_x_x:
  2270. begin
  2271. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2272. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2273. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2274. end;
  2275. in_objc_selector_x:
  2276. begin
  2277. result:=cobjcselectornode.create(left);
  2278. { reused }
  2279. left:=nil;
  2280. end;
  2281. in_objc_protocol_x:
  2282. begin
  2283. result:=cobjcprotocolnode.create(left);
  2284. { reused }
  2285. left:=nil;
  2286. end;
  2287. in_objc_encode_x:
  2288. begin
  2289. result:=handle_objc_encode;
  2290. end;
  2291. else
  2292. internalerror(8);
  2293. end;
  2294. end;
  2295. if not assigned(result) and not
  2296. codegenerror then
  2297. result:=simplify;
  2298. end;
  2299. function tinlinenode.pass_1 : tnode;
  2300. var
  2301. hp,hpp,resultnode : tnode;
  2302. shiftconst: longint;
  2303. tempnode: ttempcreatenode;
  2304. newstatement: tstatementnode;
  2305. newblock: tblocknode;
  2306. begin
  2307. result:=nil;
  2308. { if we handle writeln; left contains no valid address }
  2309. if assigned(left) then
  2310. begin
  2311. if left.nodetype=callparan then
  2312. tcallparanode(left).firstcallparan
  2313. else
  2314. firstpass(left);
  2315. end;
  2316. { intern const should already be handled }
  2317. if nf_inlineconst in flags then
  2318. internalerror(200104044);
  2319. case inlinenumber of
  2320. in_lo_qword,
  2321. in_hi_qword,
  2322. in_lo_long,
  2323. in_hi_long,
  2324. in_lo_word,
  2325. in_hi_word:
  2326. begin
  2327. shiftconst := 0;
  2328. case inlinenumber of
  2329. in_hi_qword:
  2330. shiftconst := 32;
  2331. in_hi_long:
  2332. shiftconst := 16;
  2333. in_hi_word:
  2334. shiftconst := 8;
  2335. end;
  2336. if shiftconst <> 0 then
  2337. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2338. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2339. else
  2340. result := ctypeconvnode.create_internal(left,resultdef);
  2341. left := nil;
  2342. firstpass(result);
  2343. end;
  2344. in_sizeof_x:
  2345. begin
  2346. expectloc:=LOC_REGISTER;
  2347. end;
  2348. in_typeof_x:
  2349. begin
  2350. expectloc:=LOC_REGISTER;
  2351. end;
  2352. in_length_x:
  2353. begin
  2354. if is_shortstring(left.resultdef) then
  2355. expectloc:=left.expectloc
  2356. else
  2357. begin
  2358. { ansi/wide string }
  2359. expectloc:=LOC_REGISTER;
  2360. end;
  2361. end;
  2362. in_typeinfo_x:
  2363. begin
  2364. expectloc:=LOC_REGISTER;
  2365. end;
  2366. in_assigned_x:
  2367. begin
  2368. expectloc := LOC_JUMP;
  2369. end;
  2370. in_pred_x,
  2371. in_succ_x:
  2372. begin
  2373. expectloc:=LOC_REGISTER;
  2374. end;
  2375. in_setlength_x,
  2376. in_initialize_x,
  2377. in_finalize_x:
  2378. begin
  2379. expectloc:=LOC_VOID;
  2380. end;
  2381. in_inc_x,
  2382. in_dec_x:
  2383. begin
  2384. expectloc:=LOC_VOID;
  2385. { range/overflow checking doesn't work properly }
  2386. { with the inc/dec code that's generated (JM) }
  2387. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2388. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2389. trigger an overflow. For uint32 it works because then the operation is done
  2390. in 64bit. Range checking is not applicable to pointers either }
  2391. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2392. { convert to simple add (JM) }
  2393. begin
  2394. newblock := internalstatements(newstatement);
  2395. { extra parameter? }
  2396. if assigned(tcallparanode(left).right) then
  2397. begin
  2398. { Yes, use for add node }
  2399. hpp := tcallparanode(tcallparanode(left).right).left;
  2400. tcallparanode(tcallparanode(left).right).left := nil;
  2401. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2402. CGMessage(parser_e_illegal_expression);
  2403. end
  2404. else
  2405. begin
  2406. { no, create constant 1 }
  2407. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2408. end;
  2409. typecheckpass(hpp);
  2410. if not((hpp.resultdef.typ=orddef) and
  2411. {$ifndef cpu64bitaddr}
  2412. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2413. {$else not cpu64bitaddr}
  2414. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2415. {$endif not cpu64bitaddr}
  2416. inserttypeconv_internal(hpp,sinttype);
  2417. { make sure we don't call functions part of the left node twice (and generally }
  2418. { optimize the code generation) }
  2419. if node_complexity(tcallparanode(left).left) > 1 then
  2420. begin
  2421. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2422. addstatement(newstatement,tempnode);
  2423. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2424. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2425. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2426. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2427. end
  2428. else
  2429. begin
  2430. hp := tcallparanode(left).left.getcopy;
  2431. tempnode := nil;
  2432. end;
  2433. resultnode := hp.getcopy;
  2434. { avoid type errors from the addn/subn }
  2435. if not is_integer(resultnode.resultdef) then
  2436. begin
  2437. inserttypeconv_internal(hp,sinttype);
  2438. inserttypeconv_internal(hpp,sinttype);
  2439. end;
  2440. { addition/substraction depending on inc/dec }
  2441. if inlinenumber = in_inc_x then
  2442. hpp := caddnode.create(addn,hp,hpp)
  2443. else
  2444. hpp := caddnode.create(subn,hp,hpp);
  2445. { assign result of addition }
  2446. if not(is_integer(resultnode.resultdef)) then
  2447. inserttypeconv(hpp,torddef.create(
  2448. {$ifdef cpu64bitaddr}
  2449. s64bit,
  2450. {$else cpu64bitaddr}
  2451. s32bit,
  2452. {$endif cpu64bitaddr}
  2453. get_min_value(resultnode.resultdef),
  2454. get_max_value(resultnode.resultdef)))
  2455. else
  2456. inserttypeconv(hpp,resultnode.resultdef);
  2457. { avoid any possible warnings }
  2458. inserttypeconv_internal(hpp,resultnode.resultdef);
  2459. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2460. { deallocate the temp }
  2461. if assigned(tempnode) then
  2462. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2463. { firstpass it }
  2464. firstpass(tnode(newblock));
  2465. { return new node }
  2466. result := newblock;
  2467. end;
  2468. end;
  2469. in_include_x_y,
  2470. in_exclude_x_y:
  2471. begin
  2472. expectloc:=LOC_VOID;
  2473. end;
  2474. in_pack_x_y_z,
  2475. in_unpack_x_y_z:
  2476. begin
  2477. result:=first_pack_unpack;
  2478. end;
  2479. in_exp_real:
  2480. begin
  2481. result:= first_exp_real;
  2482. end;
  2483. in_round_real:
  2484. begin
  2485. result:= first_round_real;
  2486. end;
  2487. in_trunc_real:
  2488. begin
  2489. result:= first_trunc_real;
  2490. end;
  2491. in_int_real:
  2492. begin
  2493. result:= first_int_real;
  2494. end;
  2495. in_frac_real:
  2496. begin
  2497. result:= first_frac_real;
  2498. end;
  2499. in_cos_real:
  2500. begin
  2501. result:= first_cos_real;
  2502. end;
  2503. in_sin_real:
  2504. begin
  2505. result := first_sin_real;
  2506. end;
  2507. in_arctan_real:
  2508. begin
  2509. result := first_arctan_real;
  2510. end;
  2511. in_pi_real :
  2512. begin
  2513. result := first_pi;
  2514. end;
  2515. in_abs_real:
  2516. begin
  2517. result := first_abs_real;
  2518. end;
  2519. in_abs_long:
  2520. begin
  2521. result := first_abs_long;
  2522. end;
  2523. in_sqr_real:
  2524. begin
  2525. result := first_sqr_real;
  2526. end;
  2527. in_sqrt_real:
  2528. begin
  2529. result := first_sqrt_real;
  2530. end;
  2531. in_ln_real:
  2532. begin
  2533. result := first_ln_real;
  2534. end;
  2535. {$ifdef SUPPORT_MMX}
  2536. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2537. begin
  2538. end;
  2539. {$endif SUPPORT_MMX}
  2540. in_assert_x_y :
  2541. begin
  2542. expectloc:=LOC_VOID;
  2543. end;
  2544. in_low_x,
  2545. in_high_x:
  2546. internalerror(200104047);
  2547. in_slice_x:
  2548. internalerror(2005101501);
  2549. in_ord_x,
  2550. in_chr_byte:
  2551. begin
  2552. { should not happend as it's converted to typeconv }
  2553. internalerror(200104045);
  2554. end;
  2555. in_ofs_x :
  2556. internalerror(2000101001);
  2557. in_seg_x :
  2558. internalerror(200104046);
  2559. in_settextbuf_file_x,
  2560. in_reset_typedfile,
  2561. in_rewrite_typedfile,
  2562. in_str_x_string,
  2563. in_val_x,
  2564. in_read_x,
  2565. in_readln_x,
  2566. in_write_x,
  2567. in_writeln_x :
  2568. begin
  2569. { should be handled by pass_typecheck }
  2570. internalerror(200108234);
  2571. end;
  2572. in_get_frame:
  2573. begin
  2574. include(current_procinfo.flags,pi_needs_stackframe);
  2575. expectloc:=LOC_CREGISTER;
  2576. end;
  2577. in_get_caller_frame:
  2578. begin
  2579. expectloc:=LOC_REGISTER;
  2580. end;
  2581. in_get_caller_addr:
  2582. begin
  2583. expectloc:=LOC_REGISTER;
  2584. end;
  2585. in_prefetch_var:
  2586. begin
  2587. expectloc:=LOC_VOID;
  2588. end;
  2589. in_unaligned_x:
  2590. begin
  2591. expectloc:=tcallparanode(left).left.expectloc;
  2592. end;
  2593. in_rol_x,
  2594. in_rol_x_x,
  2595. in_ror_x,
  2596. in_ror_x_x:
  2597. expectloc:=LOC_REGISTER;
  2598. else
  2599. internalerror(89);
  2600. end;
  2601. end;
  2602. {$maxfpuregisters default}
  2603. function tinlinenode.docompare(p: tnode): boolean;
  2604. begin
  2605. docompare :=
  2606. inherited docompare(p) and
  2607. (inlinenumber = tinlinenode(p).inlinenumber);
  2608. end;
  2609. function tinlinenode.first_pi : tnode;
  2610. begin
  2611. result:=crealconstnode.create(getpi,pbestrealtype^);
  2612. end;
  2613. function tinlinenode.first_arctan_real : tnode;
  2614. begin
  2615. { create the call to the helper }
  2616. { on entry left node contains the parameter }
  2617. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2618. ccallparanode.create(left,nil));
  2619. left := nil;
  2620. end;
  2621. function tinlinenode.first_abs_real : tnode;
  2622. begin
  2623. { create the call to the helper }
  2624. { on entry left node contains the parameter }
  2625. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2626. ccallparanode.create(left,nil));
  2627. left := nil;
  2628. end;
  2629. function tinlinenode.first_sqr_real : tnode;
  2630. begin
  2631. { create the call to the helper }
  2632. { on entry left node contains the parameter }
  2633. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2634. ccallparanode.create(left,nil)),resultdef);
  2635. left := nil;
  2636. end;
  2637. function tinlinenode.first_sqrt_real : tnode;
  2638. begin
  2639. { create the call to the helper }
  2640. { on entry left node contains the parameter }
  2641. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2642. ccallparanode.create(left,nil)),resultdef);
  2643. left := nil;
  2644. end;
  2645. function tinlinenode.first_ln_real : tnode;
  2646. begin
  2647. { create the call to the helper }
  2648. { on entry left node contains the parameter }
  2649. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2650. ccallparanode.create(left,nil));
  2651. left := nil;
  2652. end;
  2653. function tinlinenode.first_cos_real : tnode;
  2654. begin
  2655. { create the call to the helper }
  2656. { on entry left node contains the parameter }
  2657. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2658. ccallparanode.create(left,nil));
  2659. left := nil;
  2660. end;
  2661. function tinlinenode.first_sin_real : tnode;
  2662. begin
  2663. { create the call to the helper }
  2664. { on entry left node contains the parameter }
  2665. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2666. ccallparanode.create(left,nil));
  2667. left := nil;
  2668. end;
  2669. function tinlinenode.first_exp_real : tnode;
  2670. begin
  2671. { create the call to the helper }
  2672. { on entry left node contains the parameter }
  2673. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2674. left := nil;
  2675. end;
  2676. function tinlinenode.first_int_real : tnode;
  2677. begin
  2678. { create the call to the helper }
  2679. { on entry left node contains the parameter }
  2680. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2681. left := nil;
  2682. end;
  2683. function tinlinenode.first_frac_real : tnode;
  2684. begin
  2685. { create the call to the helper }
  2686. { on entry left node contains the parameter }
  2687. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2688. left := nil;
  2689. end;
  2690. function tinlinenode.first_round_real : tnode;
  2691. begin
  2692. { create the call to the helper }
  2693. { on entry left node contains the parameter }
  2694. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2695. left := nil;
  2696. end;
  2697. function tinlinenode.first_trunc_real : tnode;
  2698. begin
  2699. { create the call to the helper }
  2700. { on entry left node contains the parameter }
  2701. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2702. left := nil;
  2703. end;
  2704. function tinlinenode.first_abs_long : tnode;
  2705. begin
  2706. result:=nil;
  2707. end;
  2708. function tinlinenode.first_pack_unpack: tnode;
  2709. var
  2710. loopstatement : tstatementnode;
  2711. loop : tblocknode;
  2712. loopvar : ttempcreatenode;
  2713. tempnode,
  2714. source,
  2715. target,
  2716. index,
  2717. unpackednode,
  2718. packednode,
  2719. sourcevecindex,
  2720. targetvecindex,
  2721. loopbody : tnode;
  2722. temprangedef : tdef;
  2723. ulorange,
  2724. uhirange,
  2725. plorange,
  2726. phirange : TConstExprInt;
  2727. begin
  2728. { transform into a for loop which assigns the data of the (un)packed }
  2729. { array to the other one }
  2730. source := left;
  2731. if (inlinenumber = in_unpack_x_y_z) then
  2732. begin
  2733. target := tcallparanode(source).right;
  2734. index := tcallparanode(target).right;
  2735. packednode := tcallparanode(source).left;
  2736. unpackednode := tcallparanode(target).left;
  2737. end
  2738. else
  2739. begin
  2740. index := tcallparanode(source).right;
  2741. target := tcallparanode(index).right;
  2742. packednode := tcallparanode(target).left;
  2743. unpackednode := tcallparanode(source).left;
  2744. end;
  2745. source := tcallparanode(source).left;
  2746. target := tcallparanode(target).left;
  2747. index := tcallparanode(index).left;
  2748. loop := internalstatements(loopstatement);
  2749. loopvar := ctempcreatenode.create(
  2750. tarraydef(packednode.resultdef).rangedef,
  2751. tarraydef(packednode.resultdef).rangedef.size,
  2752. tt_persistent,true);
  2753. addstatement(loopstatement,loopvar);
  2754. { For range checking: we have to convert to an integer type (in case the index type }
  2755. { is an enum), add the index and loop variable together, convert the result }
  2756. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2757. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2758. { errors }
  2759. temprangedef:=nil;
  2760. getrange(unpackednode.resultdef,ulorange,uhirange);
  2761. getrange(packednode.resultdef,plorange,phirange);
  2762. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2763. sourcevecindex := ctemprefnode.create(loopvar);
  2764. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2765. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2766. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2767. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2768. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2769. if (inlinenumber = in_pack_x_y_z) then
  2770. begin
  2771. { swap source and target vec indices }
  2772. tempnode := sourcevecindex;
  2773. sourcevecindex := targetvecindex;
  2774. targetvecindex := tempnode;
  2775. end;
  2776. { create the assignment in the loop body }
  2777. loopbody :=
  2778. cassignmentnode.create(
  2779. cvecnode.create(target.getcopy,targetvecindex),
  2780. cvecnode.create(source.getcopy,sourcevecindex)
  2781. );
  2782. { create the actual for loop }
  2783. tempnode := cfornode.create(
  2784. ctemprefnode.create(loopvar),
  2785. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2786. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2787. loopbody,
  2788. false);
  2789. addstatement(loopstatement,tempnode);
  2790. { free the loop counter }
  2791. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2792. result := loop;
  2793. end;
  2794. begin
  2795. cinlinenode:=tinlinenode;
  2796. end.