exp.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. #include "std.h"
  2. #include "exp.h"
  3. #include "toker.h"
  4. using namespace CG;
  5. //**************** Expression *********************
  6. Exp::~Exp(){
  7. }
  8. //internal eval
  9. Val *Exp::_eval( Scope *sc ){
  10. fail( "TODO!" );
  11. return 0;
  12. }
  13. Val *Exp::eval( Scope *sc ){
  14. return _eval(sc);
  15. }
  16. Val *Exp::eval( Scope *sc,Type *ty ){
  17. Val *v=_eval(sc);
  18. return v->cast(ty);
  19. }
  20. Val *Exp::evalRef( Block *b ){
  21. Val *v=_eval(b);
  22. if( !v->type->refType() ) fail( "Expression must be a variable" );
  23. v=b->linearizeRef(v);
  24. return v;
  25. }
  26. Val *Exp::evalInit( Scope *sc,Type *ty ){
  27. return _eval( sc )->initCast( ty );
  28. }
  29. //************ Expression sequence ****************
  30. ExpSeq::ExpSeq(){
  31. }
  32. ExpSeq::~ExpSeq(){
  33. }
  34. //************** Cast Expression ******************
  35. Val *CastExp::_eval( Scope *sc ){
  36. Val *v=exp->eval(sc);
  37. v=v->explicitCast( type );
  38. return v;
  39. }
  40. //*************** Val Expression ******************
  41. Val *ValExp::_eval( Scope *sc ){
  42. return val;
  43. }
  44. //************ Local Decl Expression **************
  45. Val *LocalDeclExp::_eval( Scope *sc ){
  46. Block *b=dynamic_cast<Block*>(sc);
  47. assert(b);
  48. Val *v=new Val(type,tmp(type->cgType()));
  49. Decl *d=new Decl( ident,v );
  50. Val *i=Val::null(type);
  51. FunBlock *func=b->fun_block;
  52. if( strictMode ){
  53. b->initRef( v,i );
  54. block->declLocal( d );
  55. }else{
  56. b->assignRef( v,i );
  57. func->declLocal( d );
  58. }
  59. if( !strictMode || opt_debug ){
  60. func->cg_enter->push_back( mov(v->cg_exp,i->cg_exp) );
  61. }
  62. return v;
  63. }
  64. //************* Global Expression *****************
  65. Val *GlobalExp::_eval( Scope *sc ){
  66. Val *v=mainFun->find(ident);
  67. if( !v ) badid( ident );
  68. return v;
  69. }
  70. //************** Ident Expression *****************
  71. Val *IdentExp::_eval( Scope *sc ){
  72. Val *v=sc->find(ident);
  73. if( !v ){
  74. if( !strictMode ){
  75. if( Block *b=dynamic_cast<Block*>(sc) ){
  76. FunBlock *f=b->fun_block;
  77. Type *ty=new RefType(type ? type : Type::int32);
  78. v=new Val(ty,tmp(ty->cgType()));
  79. f->cg_enter->push_back( mov(v->cg_exp,(new Val(Type::null,0))->cast(ty)->cg_exp) );
  80. f->declLocal(new Decl(ident,v));
  81. return v;
  82. }
  83. }
  84. badid(ident);
  85. }
  86. if( !v->cg_exp ) fail( "Identifier '%s' is not a legal expression",ident.c_str() );
  87. if( type ){
  88. Type *ty=v->type;
  89. if( FunType *t=ty->funType() ) ty=t->return_type;
  90. else if( ArrayType *t=ty->arrayType() ) ty=t->element_type;
  91. if( !type->equals(ty) ) fail( "Identifier type does not match declared type" );
  92. }
  93. return v;
  94. }
  95. //************** Member Expression ****************
  96. Val *MemberExp::_eval( Scope *sc ){
  97. return rhs->eval( lhs->eval( sc ) );
  98. }
  99. //*************** Null Expression *****************
  100. Val *NullExp::_eval( Scope *sc ){
  101. return new Val( Type::null,0 );
  102. }
  103. //*************** Self Expression *****************
  104. Val *SelfExp::_eval( Scope *sc ){
  105. Block *b=dynamic_cast<Block*>(sc);
  106. Val *v=b ? b->fun_block->fun_scope : 0;
  107. if( !v ) fail( "'Self' can only be used within methods" );
  108. return v;
  109. }
  110. //************** Super Expression *****************
  111. Val *SuperExp::_eval( Scope *sc ){
  112. Block *b=dynamic_cast<Block*>(sc);
  113. Val *v=b ? b->fun_block->fun_scope : 0;
  114. if( !v ) fail( "'Super' can only be used within methods" );
  115. return new SuperVal( v );
  116. }
  117. //*************** New Expression ******************
  118. Val *NewExp::_eval( Scope *sc ){
  119. Val *v=exp->eval(sc);
  120. ClassType *t=v->type->classType();
  121. if( !t ){
  122. ObjectType *o=v->type->objectType();
  123. if( !o ) fail( "Subexpression for 'New' must be a user defined type or object" );
  124. t=o->class_val->type->classType();
  125. assert(t);
  126. v=new Val( t,mem(CG_PTR,v->cg_exp,0) );
  127. }else if( t->attrs & ClassType::ABSTRACT ){
  128. string tyname="?";
  129. if( IdentExp *ie=dynamic_cast<IdentExp*>(exp) ) tyname=ie->ident;
  130. set<string> ids;
  131. while( t ){
  132. int k;
  133. for( k=0;k<t->methods.size();++k ){
  134. Decl *d=t->methods[k];
  135. if( ids.count(d->ident) ) continue;
  136. if( FunType *f=d->val->type->funType() ){
  137. if( f->attrs & FunType::ABSTRACT ){
  138. fail( "Unable to create new object of abstract type '%s' due to abstract method '%s'",tyname.c_str(),d->ident.c_str() );
  139. }
  140. }
  141. ids.insert(d->ident);
  142. }
  143. t=t->superClass();
  144. }
  145. fail( "Unable to create new object of abstract type '%s'",tyname.c_str() );
  146. }else if( t->attrs & ClassType::EXTERN ){
  147. fail( "'New' can not be used to create extern objects" );
  148. }
  149. Type *type=new ObjectType(v);
  150. v=new Val(type,jsr(CG_PTR,"bbObjectNew",v->cg_exp));
  151. return v;
  152. }
  153. //************** Array Expression *****************
  154. Val *ArrayExp::_eval( Scope *sc ){
  155. if( !type ){
  156. Val *v=exp->eval(sc);
  157. type=v->type;
  158. if( !type->classType() ) fail( "Subexpression for 'New array' must be a Type" );
  159. type=new ObjectType(v);
  160. }
  161. ArrayType *arr=new ArrayType( type,dims.size() );
  162. string e=type->encoding();
  163. if( e.size() && e[0]==':' && type->exObjectType() ) e='?'+e.substr(1);
  164. CGDat *d=dat();
  165. d->push_back( lit(tobstring(e),CG_CSTRING) );
  166. CGJsr *cg;
  167. if( dims.size()==1 ){
  168. cg=jsr(CG_PTR,"bbArrayNew1D",d,dims[0]->eval(sc,Type::int32)->cg_exp);
  169. }else{
  170. cg=jsr(CG_PTR,"bbArrayNew",d,lit((int)dims.size()) );
  171. for( int k=0;k<dims.size();++k ){
  172. cg->args.push_back( dims[k]->eval(sc,Type::int32)->cg_exp );
  173. }
  174. }
  175. Val *v=new Val(arr,cg);
  176. return v;
  177. }
  178. //************ AutoArray Expression ***************
  179. Val *ArrayDataExp::_eval( Scope *sc ){
  180. if( !exps.size() ) return new Val( Type::null,0 );
  181. int k;
  182. Type *ty=0;
  183. vector<Val*> vals;
  184. for( k=0;k<exps.size();++k ){
  185. Val *v=exps[k]->eval(sc);
  186. if( !v->type || v->type->nullType() ){
  187. fail( "Auto array element has no type" );
  188. }
  189. if( !ty ){
  190. ty=v->type;
  191. }else{
  192. if( !ty->equals(v->type) ) fail( "Auto array elements must have identical types" );
  193. }
  194. vals.push_back( v );
  195. }
  196. vector<CGStm*> stms;
  197. CGTmp *t=tmp(CG_PTR);
  198. stms.push_back( mov(t,jsr(CG_PTR,"bbArrayNew1D",genCString(ty->encoding()),lit((int)vals.size()))) );
  199. for( k=0;k<vals.size();++k ){
  200. Val *v=vals[k]->cast( ty );
  201. if( ty->objectType() && !opt_threaded ){
  202. v=v->retain();
  203. }
  204. stms.push_back( mov(mem(ty->cgType(),t,k*ty->size()+24),v->cg_exp) );
  205. }
  206. return new Val( new ArrayType(ty,1),esq( seq(stms),t ) );
  207. }
  208. //************ Intrinsic Expression ***************
  209. Val *IntrinsicExp::_eval( Scope *sc ){
  210. if( toke==T_VARPTR ){
  211. if( type ) fail( "No return type permitted for 'Varptr'" );
  212. Val *v=exp->eval(sc);
  213. RefType *t=v->type->refType();
  214. if( !t ) fail( "Subexpression for 'Ptr' must be a variable" );
  215. Type *type=new PtrType(t->val_type);
  216. return new Val(type,lea(v->cg_exp));
  217. }else if( toke==T_LEN ){
  218. if( type && type->encoding()!="i" ) fail( "Return type for 'Len' must be Int" );
  219. Val *v=exp->eval(sc);
  220. if( v->type->stringType() ){
  221. if( v->constant() ) return new Val((int)v->stringValue().size());
  222. return new Val(Type::int32,mem(CG_INT32,v->cg_exp,8));
  223. }else if( v->type->arrayType() ){
  224. return new Val(Type::int32,mem(CG_INT32,v->cg_exp,20));
  225. }
  226. return new Val(Type::int32,lit1);
  227. }else if( toke==T_SIZEOF ){
  228. if( type && type->encoding()!="i" ) fail( "Return type for 'SizeOf' must be Int" );
  229. Val *v=exp->eval(sc);
  230. if( v->type->stringType() ){
  231. if( v->constant() ) return new Val((int)v->stringValue().size()*2);
  232. return new Val(Type::int32,bop(CG_MUL,mem(CG_INT32,v->cg_exp,8),lit(2)));
  233. }else if( v->type->arrayType() ){
  234. return new Val(Type::int32,mem(CG_INT32,v->cg_exp,16));
  235. }else if( ObjectType *t=v->type->objectType() ){
  236. return new Val(Type::int32,lit(t->objectClass()->sizeof_fields-8));
  237. }else if( ClassType *t=v->type->classType() ){
  238. if( t->attrs & ClassType::EXTERN ){
  239. }else{
  240. return new Val(Type::int32,lit(t->sizeof_fields-8));
  241. }
  242. }
  243. return new Val(Type::int32,lit(v->type->size()));
  244. }else if( toke==T_CHR ){
  245. if( type && type->encoding()!="$" ) fail( "Return type for 'Chr' must be String" );
  246. Val *v=exp->eval(sc,Type::int32);
  247. if( v->constant() ){
  248. return new Val( bstring(1,(bchar_t)v->intValue()) );
  249. }
  250. return new Val( Type::stringObject,jsr(CG_PTR,"bbStringFromChar",v->cg_exp) );
  251. }else if( toke==T_ASC ){
  252. if( type && type->encoding()!="i" ) fail( "Return type for 'Asc' must be Int" );
  253. Val *v=exp->eval(sc,Type::stringObject);
  254. if( v->constant() ){
  255. if( v->stringValue().size() ) return new Val( (int)v->stringValue()[0] );
  256. return new Val(-1);
  257. }
  258. return new Val( Type::int32,jsr(CG_INT32,"bbStringAsc",v->cg_exp) );
  259. }else if( toke==T_INCBINPTR ){
  260. if( type && type->encoding()!="*b" ) fail( "Return type for 'IncbinPtr' must be Byte Ptr" );
  261. Val *v=exp->eval(sc,Type::stringObject);
  262. return new Val( Type::bytePtr,jsr(CG_PTR,"bbIncbinPtr",v->cg_exp) );
  263. }else if( toke==T_INCBINLEN ){
  264. if( type && type->encoding()!="i" ) fail( "Return type for 'IncbinLen' must be Int" );
  265. Val *v=exp->eval(sc,Type::stringObject);
  266. return new Val( Type::int32,jsr(CG_INT32,"bbIncbinLen",v->cg_exp) );
  267. }else{
  268. fail( "Internal error" );
  269. }
  270. return 0;
  271. }
  272. //************* ExpSeq Expression *****************
  273. Val *ExpSeqExp::_eval( Scope *sc ){
  274. assert(0);
  275. return 0;
  276. }
  277. Val *ExpSeqExp::invokeFun( Val *v,FunType *fun,Scope *sc ){
  278. if( seq.size()>fun->args.size() ) fail( "Too many function parameters" );
  279. vector<Val*> args;
  280. vector<CGExp*> cg_args;
  281. CGSeq *cleanup=CG::seq(0);
  282. int k;
  283. for( k=0;k<fun->args.size();++k ){
  284. Decl *d=fun->args[k];
  285. Type *ty=d->val->type;
  286. if( k<seq.size() && seq[k] ){
  287. Val *t=seq[k]->eval(sc)->funArgCast(ty,cleanup);
  288. args.push_back(t);
  289. }else if( CGExp *e=d->val->cg_exp ){
  290. args.push_back( new Val(d->val->type,e) );
  291. }else{
  292. fail( "Missing function parameter '%s'",d->ident.c_str() );
  293. }
  294. cg_args.push_back( args.back()->cg_exp );
  295. }
  296. Type *ty=fun->return_type;
  297. CGExp *e=jsr( ty->cgType(),fun->call_conv,v->cg_exp,cg_args );
  298. if( ty->cstringType() ){
  299. ty=Type::stringObject;
  300. e=jsr( CG_PTR,"bbStringFromCString",e );
  301. }else if( ty->wstringType() ){
  302. ty=Type::stringObject;
  303. e=jsr( CG_PTR,"bbStringFromWString",e );
  304. }
  305. if( cleanup->stms.size() ){
  306. CGTmp *t=tmp( ty->cgType() );
  307. CGSeq *tseq=CG::seq(0);
  308. tseq->push_back( mov(t,e) );
  309. tseq->push_back( cleanup );
  310. e=esq( tseq,t );
  311. }
  312. return new Val( ty,e );
  313. }
  314. Val *ExpSeqExp::performCast( Val *lhs,Scope *sc ){
  315. if( seq.size()!=1 ) fail( "Illegal subexpression for object cast" );
  316. Val *rhs=seq[0]->eval(sc);
  317. return rhs->explicitCast( new ObjectType(lhs) );
  318. }
  319. Val *ExpSeqExp::indexString( Val *v,Scope *sc ){
  320. if( seq.size()!=1 ) fail( "Illegal subexpression for string index" );
  321. CGExp *p=v->cg_exp;
  322. CGExp *e=seq[0]->eval( sc,Type::int32 )->cg_exp;
  323. if( opt_debug ){
  324. p=tmp(CG_PTR);
  325. CGTmp *t=tmp(CG_INT32);
  326. CGSym *q=sym();
  327. CGStm *stms=CG::seq(
  328. mov(p,v->cg_exp),
  329. mov(t,e),
  330. bcc(CG_LTU,t,mem(CG_INT32,p,8),q),
  331. eva(jsr(CG_INT32,"brl_blitz_ArrayBoundsError")),
  332. lab(q),
  333. 0 );
  334. e=esq(stms,t);
  335. }
  336. e=cvt( CG_INT32,mem( CG_INT16,bop(CG_ADD,p,bop(CG_MUL,e,lit(2))),12 ) );
  337. return new Val(Type::int32,e);
  338. // Val *i=seq[0]->eval( sc,Type::int32 );
  339. // CGExp *e=cvt( CG_INT32,mem( CG_INT16,bop(CG_ADD,v->cg_exp,bop(CG_MUL,i->cg_exp,lit(2))),12 ) );
  340. // return new Val(Type::int32,e);
  341. }
  342. Val *ExpSeqExp::indexArray( Val *v,ArrayType *arr,Scope *sc ){
  343. if( arr->dims!=seq.size() ) fail( "Incorrect number of array dimensions" );
  344. CGExp *cg_exp=v->cg_exp,*p=0;
  345. bool sidefx=cg_exp->sideEffects();
  346. if( sidefx ) cg_exp=tmp(CG_PTR);
  347. for( int k=0;k<seq.size();++k ){
  348. Val *v=seq[k]->eval(sc)->cast( Type::int32 );
  349. CGExp *e=v->cg_exp;
  350. if( k<seq.size()-1 ) e=bop(CG_MUL,e,mem(CG_INT32,cg_exp,k*4+24));
  351. if( opt_debug ){
  352. CGTmp *t=tmp(CG_INT32);
  353. CGSym *q=sym();
  354. CGStm *stms=CG::seq(
  355. mov(t,e),
  356. bcc(CG_LTU,t,mem(CG_INT32,cg_exp,k*4+20),q),
  357. eva(jsr(CG_INT32,"brl_blitz_ArrayBoundsError")),
  358. lab(q),
  359. 0 );
  360. e=esq(stms,t);
  361. }
  362. if( p ) p=bop(CG_ADD,p,e);
  363. else p=e;
  364. }
  365. Type *ty=arr->element_type;
  366. p=bop(CG_ADD,cg_exp,bop(CG_MUL,p,lit(ty->size())));
  367. if( sidefx ) p=esq( mov(cg_exp,v->cg_exp),p );
  368. p=mem(ty->cgType(),p,seq.size()*4+20);
  369. return new Val(ty,p);
  370. }
  371. Val *ExpSeqExp::indexPointer( Val *v,Type *t,Scope *sc ){
  372. if( seq.size()!=1 ) fail( "Illegal subexpression for pointer index" );
  373. Val *i=seq[0]->eval( sc,Type::int32 );
  374. CGExp *e=mem( t->cgType(),
  375. bop(CG_ADD,v->cg_exp,
  376. bop(CG_MUL,i->cg_exp,lit(t->size()))),0);
  377. Type *type=new RefType(t);
  378. return new Val(type,e);
  379. }
  380. //************* Invoke Expression *****************
  381. Val *InvokeExp::_eval( Scope *sc ){
  382. Val *v=exp->eval(sc);
  383. Type *type=v->type;
  384. if( FunType *t=type->funType() ){
  385. return invokeFun( v,t,sc );
  386. }else if( ClassType *t=type->classType() ){
  387. return performCast( v,sc );
  388. }else if( ArrayType *t=type->arrayType() ){
  389. if( !strictMode ) return indexArray( v,t,sc );
  390. }
  391. if( !strictMode ){
  392. if( IdentExp *ie=dynamic_cast<IdentExp*>(exp) ){
  393. fail( "Identifier '%s' not found",ie->ident.c_str() );
  394. }
  395. }
  396. fail( "Expression of type '%s' cannot be invoked",type->toString().c_str() );
  397. return 0;
  398. }
  399. //************** Index Expression *****************
  400. Val *IndexExp::_eval( Scope *sc ){
  401. Val *v=exp->eval(sc);
  402. Type *type=v->type;
  403. if( ArrayType *t=type->arrayType() ){
  404. return indexArray( v,t,sc );
  405. }else if( PtrType *t=type->ptrType() ){
  406. return indexPointer( v,t->val_type,sc );
  407. }else if( type->stringType() ){
  408. return indexString( v,sc );
  409. }
  410. fail( "Expression of type '%s' cannot be indexed",type->toString().c_str() );
  411. return 0;
  412. }
  413. //************** Slice Expression *****************
  414. Val *SliceExp::_eval( Scope *sc ){
  415. Val *v=exp->eval(sc);
  416. ArrayType *arr=v->type->arrayType();
  417. StringType *str=v->type->stringType();
  418. if( (!arr && !str) || (arr && arr->dims!=1) ) fail( "Slices can only be used with strings or one dimensional arrays" );
  419. CGExp *e=v->cg_exp,*t=0;
  420. if( !rhs && v->cg_exp->sideEffects() ){
  421. t=e;
  422. e=tmp(CG_PTR);
  423. }
  424. CGExp *l=lhs ? lhs->eval(sc,Type::int32)->cg_exp : lit0;
  425. CGExp *r=rhs ? rhs->eval(sc,Type::int32)->cg_exp : mem(CG_INT32,e,arr ? 20 : 8);
  426. if( str ){
  427. l=jsr(CG_PTR,"bbStringSlice",e,l,r);
  428. }else{
  429. CGExp *ty=genCString( arr->element_type->encoding() );
  430. l=jsr(CG_PTR,"bbArraySlice",ty,e,l,r);
  431. }
  432. Type *ty=v->type;
  433. if( RefType *r=ty->refType() ) ty=r->val_type;
  434. if( !t ) return new Val( ty,l );
  435. return new Val( ty,esq(mov(e,t),l) );
  436. }
  437. //************* Compare Expression ****************
  438. Val *CmpExp::_eval( Scope *sc ){
  439. Val *l=lhs->eval(sc);
  440. Val *r=rhs->eval(sc);
  441. if( l->type->ptrType() && r->type->nullType() ) r=r->cast(l->type);
  442. else if( r->type->ptrType() && l->type->nullType() ) l=l->cast(r->type);
  443. if( PtrType *px=l->type->ptrType() ){
  444. if( PtrType *py=r->type->ptrType() ){
  445. if( px->val_type->equals( py->val_type ) ){
  446. int cgop;
  447. switch( op ){
  448. case T_LT:cgop=CG_LT;break;
  449. case T_EQ:cgop=CG_EQ;break;
  450. case T_GT:cgop=CG_GT;break;
  451. case T_LE:cgop=CG_LE;break;
  452. case T_GE:cgop=CG_GE;break;
  453. case T_NE:cgop=CG_NE;break;
  454. }
  455. return new Val(Type::int32,scc(cgop,l->cg_exp,r->cg_exp));
  456. }
  457. }
  458. }
  459. if( l->type->ptrType() || r->type->ptrType() ){
  460. fail( "Pointer type mismatch" );
  461. }
  462. Type *ty=l->balance(r);
  463. if( ty->numericType() ){
  464. }else if( ty->stringType() ){
  465. }else if( ObjectType *obj=ty->objectType() ){
  466. if( op!=T_EQ && op!=T_NE ) fail( "Operator '%s' cannot be applied to objects",Toker::toString(op).c_str() );
  467. }else if( ObjectType *obj=ty->exObjectType() ){
  468. if( op!=T_EQ && op!=T_NE ) fail( "Operator '%s' cannot be applied to extern objects",Toker::toString(op).c_str() );
  469. }else if( FunType *fun=ty->funType() ){
  470. if( (op!=T_EQ && op!=T_NE) || (fun->attrs & FunType::METHOD) ){
  471. const char *p=(fun->attrs & FunType::METHOD) ? "methods" : "functions";
  472. fail( "Operator '%s' cannot be applied to %s",Toker::toString(op).c_str(),p );
  473. }
  474. }else if( ty->nullType() ){
  475. if( op!=T_EQ && op!=T_NE ) fail( "Operator '%s' cannot be applied to 'Null'",Toker::toString(op).c_str() );
  476. return new Val( op==T_EQ ? 1 : 0);
  477. }else{
  478. fail( "Operands cannot be compared" );
  479. }
  480. l=l->cast(ty);
  481. r=r->cast(ty);
  482. if( l->constant() && r->constant() ){
  483. int z;
  484. if( ty->intType() ){
  485. int64 x=l->intValue(),y=r->intValue();
  486. switch(op){
  487. case T_LT:z=x<y;break;
  488. case T_EQ:z=x==y;break;
  489. case T_GT:z=x>y;break;
  490. case T_LE:z=x<=y;break;
  491. case T_GE:z=x>=y;break;
  492. case T_NE:z=x!=y;break;
  493. }
  494. }else if( ty->floatType() ){
  495. double x=l->floatValue(),y=r->floatValue();
  496. switch(op){
  497. case T_LT:z=x<y;break;
  498. case T_EQ:z=x==y;break;
  499. case T_GT:z=x>y;break;
  500. case T_LE:z=x<=y;break;
  501. case T_GE:z=x>=y;break;
  502. case T_NE:z=x!=y;break;
  503. }
  504. }else if( ty->stringType() ){
  505. bstring x=l->stringValue(),y=r->stringValue();
  506. switch(op){
  507. case T_LT:z=x<y;break;
  508. case T_EQ:z=x==y;break;
  509. case T_GT:z=x>y;break;
  510. case T_LE:z=x<=y;break;
  511. case T_GE:z=x>=y;break;
  512. case T_NE:z=x!=y;break;
  513. }
  514. }else{
  515. fail( "Operands cannot be compared" );
  516. }
  517. return new Val(z);
  518. }
  519. int cgop;
  520. switch( op ){
  521. case T_LT:cgop=CG_LT;break;
  522. case T_EQ:cgop=CG_EQ;break;
  523. case T_GT:cgop=CG_GT;break;
  524. case T_LE:cgop=CG_LE;break;
  525. case T_GE:cgop=CG_GE;break;
  526. case T_NE:cgop=CG_NE;break;
  527. }
  528. Val *t;
  529. if( ty->stringType() ){
  530. t=new Val(Type::int32,scc(cgop,jsr(CG_INT32,"bbStringCompare",l->cg_exp,r->cg_exp),lit0));
  531. }else{
  532. t=new Val(Type::int32,scc(cgop,l->cg_exp,r->cg_exp));
  533. }
  534. return t;
  535. }
  536. //************ Short Circuit Expression ***********
  537. Val *ShortCircExp::_eval( Scope *sc ){
  538. Val *lv=lhs->eval(sc)->cond();
  539. Val *rv=rhs->eval(sc)->cond();
  540. int cg_op=op==T_AND ? CG_EQ : CG_NE;
  541. //result reg
  542. CGSym *e=sym();
  543. CGTmp *r=tmp(CG_INT32);
  544. CGStm *stms=seq(
  545. mov(r,lv->cg_exp),
  546. bcc(cg_op,r,lit0,e),
  547. mov(r,rv->cg_exp),
  548. lab(e),
  549. 0 );
  550. return new Val(Type::int32,esq(stms,r));
  551. }
  552. //***************** Not Expression ****************
  553. Val *NotExp::_eval( Scope *sc ){
  554. Val *v=exp->eval(sc)->cond();
  555. if( v->constant() ) return new Val( !v->intValue() );
  556. return new Val(Type::int32,scc(CG_EQ,v->cg_exp,lit0));
  557. }
  558. //**************** Unary Expression ***************
  559. Val *UnaryExp::_eval( Scope *sc ){
  560. Val *v=exp->eval(sc);
  561. Type *ty=v->type;
  562. if( !ty->numericType() ){
  563. fail( "Subexpression for '%s' must be of numeric type",Toker::toString(op).c_str() );
  564. }
  565. switch( op ){
  566. case '+':
  567. return v;
  568. case '~':
  569. if( !v->type->intType() ) fail( "Bitwise complement can only be used with integers" );
  570. if( v->type->cgType()!=CG_INT64 ) v=v->cast( Type::int32 );
  571. break;
  572. }
  573. ty=v->type;
  574. if( v->constant() ){
  575. if( ty->intType() ){
  576. int64 n=v->intValue();
  577. switch( op ){
  578. case '-':n=-n;break;
  579. case '~':n=~n;break;
  580. case T_ABS:n=(n>=0) ? n : -n;break;
  581. case T_SGN:n=(n==0) ? 0 : (n>0 ? 1 : -1);break;
  582. default:assert(0);
  583. }
  584. return new Val(n,ty);
  585. }else{
  586. double n=v->floatValue();
  587. switch( op ){
  588. case '-':n=-n;break;
  589. case T_ABS:n=fabs(n);break;
  590. case T_SGN:n=(n==0) ? 0 : (n>0 ? 1 : -1);break;
  591. default:assert(0);
  592. }
  593. return new Val(n,ty);
  594. }
  595. assert(0);
  596. }
  597. int cgop;
  598. switch( op ){
  599. case '-':cgop=CG_NEG;break;
  600. case '~':cgop=CG_NOT;break;
  601. case T_ABS:cgop=CG_ABS;break;
  602. case T_SGN:cgop=CG_SGN;break;
  603. default:assert(0);
  604. }
  605. return new Val(ty,uop(cgop,v->cg_exp));
  606. }
  607. //**************** Arith Expression ***************
  608. Val *ArithExp::pointerArith( Val *lv,Val *rv ){
  609. PtrType *lp=lv->type->ptrType();
  610. PtrType *rp=rv->type->ptrType();
  611. PtrType *ty=lp ? lp : rp;
  612. CGLit *sz=lit( ty->val_type->size() );
  613. if( lp && rp ){
  614. if( op!='-' ) fail( "Illegal pointer arithmetic operator" );
  615. if( !lp->equals(rp) ) fail( "Pointer types are not equivalent" );
  616. CGExp *e=bop(CG_DIV,bop(CG_SUB,lv->cg_exp,rv->cg_exp),sz);
  617. return new Val(Type::int32,e);
  618. }
  619. CGExp *e=0;
  620. if( lp ){
  621. rv=rv->cast(Type::int32);
  622. if( op=='+' ) e=bop(CG_ADD,lv->cg_exp,bop(CG_MUL,rv->cg_exp,sz));
  623. else if( op=='-' ) e=bop(CG_SUB,lv->cg_exp,bop(CG_MUL,rv->cg_exp,sz));
  624. }else{
  625. lv=lv->cast(Type::int32);
  626. if( op=='+' ) e=bop(CG_ADD,rv->cg_exp,bop(CG_MUL,lv->cg_exp,sz));
  627. }
  628. if( !e ) fail( "Illegal pointer arithmetic operator" );
  629. return new Val( ty,e );
  630. }
  631. Val *ArithExp::_eval( Scope *sc ){
  632. Val *l=lhs ? lhs->eval(sc) : lhs_val;
  633. Val *r=rhs ? rhs->eval(sc) : rhs_val;
  634. if( l->type->ptrType() || r->type->ptrType() ) return pointerArith(l,r);
  635. Type *ty=0;
  636. if( op=='^' ){
  637. ty=Type::float64;
  638. }else if( ArrayType *p=l->type->arrayType() ){
  639. if( ArrayType *q=r->type->arrayType() ){
  640. if( p->dims==q->dims ){
  641. if( p->element_type->equals( q->element_type ) ){
  642. ty=p;
  643. }else if( ObjectType *pt=p->element_type->objectType() ){
  644. if( ObjectType *qt=q->element_type->objectType() ){
  645. if( pt->extends(qt) ){
  646. ty=q;
  647. }else if( qt->extends(pt) ){
  648. ty=p;
  649. }else{
  650. ty=new ArrayType( Type::objectObject,p->dims );
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. if( !ty ) ty=l->balance(r);
  658. if( ty->nullType() ){
  659. fail( "Operator '%s' cannot be used with null",Toker::toString(op).c_str() );
  660. }else if( ty->arrayType() ){
  661. if( op!='+' ) fail( "Operator '%s' can not be used with arrays",Toker::toString(op).c_str() );
  662. }else if( ty->stringType() ){
  663. if( op!='+' ) fail( "Operator '%s' can not be used with strings",Toker::toString(op).c_str() );
  664. }else if( !ty->numericType() ){
  665. fail( "Operator '%s' can only be used with numeric types",Toker::toString(op).c_str() );
  666. }
  667. l=l->cast(ty);
  668. r=r->cast(ty);
  669. if( l->constant() && r->constant() ){
  670. if( ty->intType() ){
  671. int64 x=l->intValue(),y=r->intValue();
  672. switch( op ){
  673. case '+':x+=y;break;
  674. case '-':x-=y;break;
  675. case '*':x*=y;break;
  676. case '/':if( !y ) fail( "Integer division by zero" );x/=y;break;
  677. case '^':x=(int)pow((double)x,(double)y);break;
  678. case T_MOD:if( !y ) fail( "Integer division by zero" );x%=y;break;
  679. case T_MIN:x=x<y ? x : y;break;
  680. case T_MAX:x=x>y ? x : y;break;
  681. default:assert(0);
  682. }
  683. return new Val(x,ty);
  684. }else if( ty->floatType() ){
  685. double x=l->floatValue(),y=r->floatValue();
  686. switch( op ){
  687. case '+':x+=y;break;
  688. case '-':x-=y;break;
  689. case '*':x*=y;break;
  690. case '/':x/=y;break;
  691. case '^':x=pow(x,y);break;
  692. case T_MOD:x=fmod(x,y);break;
  693. case T_MIN:x=x<y ? x : y;break;
  694. case T_MAX:x=x>y ? x : y;break;
  695. default:assert(0);
  696. }
  697. return new Val(x,ty);
  698. }else if( ty->stringType() ){
  699. bstring x=l->stringValue(),y=r->stringValue();
  700. switch( op ){
  701. case '+':x+=y;break;
  702. default:assert(0);
  703. }
  704. return new Val(x);
  705. }
  706. assert(0);
  707. }
  708. if( ArrayType *t=ty->arrayType() ){
  709. if( l->type->arrayType()->dims!=1 || r->type->arrayType()->dims!=1 ){
  710. fail( "Multi-dimensional arrays can not be concatenated" );
  711. }
  712. CGExp *e=jsr(CG_PTR,"bbArrayConcat",genCString(t->element_type->encoding()),l->cg_exp,r->cg_exp );
  713. return new Val(ty,e);
  714. }
  715. if( ty->stringType() ){
  716. CGExp *e=jsr(CG_PTR,"bbStringConcat",l->cg_exp,r->cg_exp );
  717. return new Val(ty,e);
  718. }
  719. if( op=='^' ){
  720. CGExp *e=jsr(CG_FLOAT64,"bbFloatPow",l->cg_exp,r->cg_exp );
  721. return new Val(ty,e);
  722. }
  723. int cgop;
  724. switch( op ){
  725. case '+':cgop=CG_ADD;break;
  726. case '-':cgop=CG_SUB;break;
  727. case '*':cgop=CG_MUL;break;
  728. case '/':cgop=CG_DIV;break;
  729. case T_MOD:cgop=CG_MOD;break;
  730. case T_MIN:cgop=CG_MIN;break;
  731. case T_MAX:cgop=CG_MAX;break;
  732. default:assert(0);
  733. }
  734. return new Val(ty,bop(cgop,l->cg_exp,r->cg_exp));
  735. }
  736. //*************** Bitwise Expression **************
  737. Val *BitwiseExp::_eval( Scope *sc ){
  738. Val *l=lhs ? lhs->eval(sc) : lhs_val;
  739. Val *r=rhs ? rhs->eval(sc) : rhs_val;
  740. if( !l->type->intType() || !r->type->intType() ) fail( "Bitwise operators can only be used with integers" );
  741. Type *ty=Type::int32;
  742. if( l->type->cgType()==CG_INT64 || r->type->cgType()==CG_INT64) ty=Type::int64;
  743. l=l->cast(ty);
  744. r=r->cast(ty);
  745. if( l->constant() && r->constant() ){
  746. int64 x=l->intValue(),y=r->intValue();
  747. switch( op ){
  748. case '&':x&=y;break;
  749. case '|':x|=y;break;
  750. case '~':x^=y;break;
  751. case T_SHL:x<<=y;break;
  752. case T_SAR:x>>=y;break;
  753. case T_SHR:x=(unsigned)x>>(unsigned)y;break;
  754. default:assert(0);
  755. }
  756. return new Val(x,ty);
  757. }
  758. int cgop;
  759. switch( op ){
  760. case '&':cgop=CG_AND;break;
  761. case '|':cgop=CG_ORL;break;
  762. case '~':cgop=CG_XOR;break;
  763. case T_SHL:cgop=CG_SHL;break;
  764. case T_SHR:cgop=CG_SHR;break;
  765. case T_SAR:cgop=CG_SAR;break;
  766. default:assert(0);
  767. }
  768. return new Val(ty,bop(cgop,l->cg_exp,r->cg_exp));
  769. }