ninl.pas 118 KB

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