ninl.pas 118 KB

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