output.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "std.h"
  2. #include "output.h"
  3. #include "decl.h"
  4. #include "val.h"
  5. ostream &out::operator<<( ostream &out,CGExp *exp ){
  6. if( CGLit *t=exp->lit() ){
  7. if( t->isint() ){
  8. out<<fromint(t->int_value);
  9. if( t->type==CG_INT64 ) out<<":Long";
  10. }else if( t->isfloat() ){
  11. if( isnan( t->float_value ) ){
  12. out<<"nan";
  13. }else if( isinf( t->float_value ) ){
  14. out<<( t->float_value>0 ? "inf" : "-inf" );
  15. }else{
  16. out<<t->float_value;
  17. }
  18. out<<( t->type==CG_FLOAT32 ? '#' : '!' );
  19. }else{
  20. assert(0);
  21. }
  22. }else if( CGSym *t=exp->sym() ){
  23. out<<"\""<<t->value<<"\"";
  24. }else{
  25. const char *ty="";
  26. switch( exp->type ){
  27. case CG_INT8:ty=":b";break;
  28. case CG_INT16:ty=":s";break;
  29. case CG_INT32:break;
  30. case CG_INT64:ty=":l";break;
  31. case CG_PTR:ty=":p";break;
  32. case CG_FLOAT32:ty=":f";break;
  33. case CG_FLOAT64:ty=":d";break;
  34. default:
  35. cout<<"type error:"<<exp->type<<endl;
  36. assert(0);
  37. }
  38. if( CGMem *t=exp->mem() ){
  39. out<<"mem"<<ty<<"("<<t->exp;
  40. if( t->offset ) out<<","<<t->offset;
  41. out<<")";
  42. }else{
  43. fail( "Unrecognized intermediate code expression - !*#%" );
  44. }
  45. }
  46. return out;
  47. }
  48. ostream &out::operator<<( ostream &out,Type *ty ){
  49. if( RefType *t=ty->refType() ){
  50. out<<t->val_type<<'&';
  51. }else if( VarType *t=ty->varType() ){
  52. out<<t->val_type<<" Var";
  53. }else if( PtrType *t=ty->ptrType() ){
  54. out<<t->val_type<<'*';
  55. }else if( IntType *t=ty->intType() ){
  56. switch( t->size() ){
  57. case 1:out<<"@";break;
  58. case 2:out<<"@@";break;
  59. case 4:out<<"%";break;
  60. case 8:out<<"%%";break;
  61. default:assert(0);
  62. }
  63. }else if( FloatType *t=ty->floatType() ){
  64. switch( t->size() ){
  65. case 4:out<<"#";break;
  66. case 8:out<<"!";break;
  67. default:assert(0);
  68. }
  69. }else if( CStringType *t=ty->cstringType() ){
  70. out<<"$z";
  71. }else if( WStringType *t=ty->wstringType() ){
  72. out<<"$w";
  73. }else if( StringType *t=ty->stringType() ){
  74. out<<"$";
  75. }else if( ArrayType *t=ty->arrayType() ){
  76. out<<t->element_type<<'['<<string(t->dims-1,',')<<']';
  77. }else if( ObjectType *t=ty->objectType() ){
  78. if( t->ident=="<unknown>" ) fail( "export of unknown type" );
  79. string id=t->ident;
  80. ClassType *ty=t->objectClass();
  81. while( ty->attrs & ClassType::PRIVATE ){
  82. id=ty->super_name;
  83. ty=ty->superClass();
  84. }
  85. out<<':'<<id;
  86. }else if( ObjectType *t=ty->exObjectType() ){
  87. if( t->ident=="<unknown>" ) fail( "export of unknown type" );
  88. out<<':'<<t->ident;
  89. }else if( FunType *t=ty->funType() ){
  90. out<<t->return_type<<'(';
  91. for( int k=0;k<t->args.size();++k ){
  92. if( k ) out<<',';
  93. out<<t->args[k];
  94. }
  95. out<<')';
  96. if( t->attrs & FunType::ABSTRACT ) out<<'A';
  97. if( t->attrs & FunType::FINAL ) out<<'F';
  98. if( t->call_conv==CG_STDCALL ) out<<'S';
  99. }else if( ClassType *t=ty->classType() ){
  100. out<<'^';
  101. if( t->super_name.size() ) out<<t->super_name; else out<<"Null";
  102. out<<"{\n";
  103. int k;
  104. for( k=0;k<t->decls.size();++k ){
  105. Decl *d=t->decls[k];
  106. if( t->methods.find(d->ident) || t->fields.find(d->ident) ) continue;
  107. out<<d<<'\n';
  108. }
  109. for( k=0;k<t->fields.size();++k ){
  110. out<<'.'<<t->fields[k]<<'\n';
  111. }
  112. for( k=0;k<t->methods.size();++k ){
  113. FunType *ty=t->methods[k]->val->type->funType();
  114. out<<(ty->method() ? '-' : '+')<<t->methods[k]<<'\n';
  115. }
  116. out<<"}";
  117. if( t->attrs & ClassType::ABSTRACT ) out<<'A';
  118. if( t->attrs & ClassType::FINAL ) out<<'F';
  119. if( t->attrs & ClassType::EXTERN ) out<<'E';
  120. }else{
  121. fail( "Export Type '%s' failed",ty->toString().c_str() );
  122. }
  123. return out;
  124. }
  125. ostream &out::operator<<( ostream &out,Decl *d ){
  126. Val *v=d->val;
  127. out<<d->ident<<v->type;
  128. if( v->cg_exp ){
  129. if( v->type->stringType() && v->constant() ){
  130. out<<"=$\""<<tostring( escapeString( v->stringValue() ) )<<'\"';
  131. }else{
  132. out<<'='<<v->cg_exp;
  133. }
  134. }
  135. return out;
  136. }
  137. ostream &out::operator<<( ostream &out,const DeclSeq &seq ){
  138. int k;
  139. for( k=0;k<seq.size();++k ){
  140. out<<seq[k]<<'\n';
  141. }
  142. return out;
  143. }