00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef FALCON_GENCODE_H
00016 #define FALCON_GENCODE_H
00017
00018 #include <falcon/generator.h>
00019 #include <falcon/syntree.h>
00020 #include <falcon/genericlist.h>
00021 #include <falcon/genericvector.h>
00022 #include <falcon/stringstream.h>
00023
00024 namespace Falcon
00025 {
00026
00027 class LineMap;
00028
00029 class FALCON_DYN_CLASS GenCode: public Generator
00030 {
00031 typedef enum {
00032 e_parND,
00033 e_parNIL,
00034 e_parVAL,
00035 e_parVAR,
00036 e_parINT32,
00037 e_parSYM,
00038 e_parA,
00039 e_parB,
00040 e_parS1,
00041 e_parL1,
00042 e_parL2,
00043 e_parLBIND,
00044 e_parTRUE,
00045 e_parFALSE,
00046 e_parNTD32,
00047 e_parNTD64,
00048 e_parSTRID
00049 } t_paramType;
00050
00051
00052 class c_jmptag
00053 {
00054
00055 uint32 m_defs[4];
00056 List m_queries[4];
00057 GenericVector m_elifDefs;
00058 GenericVector m_elifQueries;
00059
00060 uint32 m_tries;
00061 uint32 m_offset;
00062 bool m_bIsForLast;
00063 const StmtForin* m_ForinLoop;
00064 Stream *m_stream;
00065
00066 uint32 addQuery( uint32 id, uint32 pos );
00067 void define( uint32 id );
00068
00069 public:
00070 c_jmptag( Stream *stream, uint32 offset = 0 );
00071
00072 uint32 addQueryBegin( uint32 blocks=1 ) { return addQuery( 0, blocks ); }
00073 uint32 addQueryEnd( uint32 blocks=1 ) { return addQuery( 1, blocks ); }
00074 uint32 addQueryNext( uint32 blocks=1 ) { return addQuery( 2, blocks ); }
00075 uint32 addQueryPostEnd( uint32 blocks=1 ) { return addQuery( 3, blocks ); }
00076 void defineBegin() { define( 0 ); }
00077 void defineEnd() { define( 1 ); }
00078 void defineNext() { define( 2 ); }
00079 void definePostEnd() { define( 3 ); }
00080
00081 uint32 addQueryIfElse( uint32 blocks=1) { return addQuery( 0, blocks ); }
00082 uint32 addQueryIfEnd( uint32 blocks=1 ) { return addQuery( 1, blocks ); }
00083 uint32 addQueryElif( uint32 elifID, uint32 blocks=1 );
00084 uint32 addQuerySwitchBlock( uint32 blockID, uint32 blocks=1 )
00085 {
00086 return addQueryElif( blockID, blocks );
00087 }
00088
00089 void defineIfElse() { define( 0 ); }
00090 void defineIfEnd() { define( 1 ); }
00091 void defineElif( uint32 id );
00092 void defineSwitchCase( uint32 id )
00093 {
00094 defineElif( id );
00095 }
00096
00097 void addTry( uint32 count = 1 ) { m_tries += count; }
00098 void removeTry( uint32 count = 1 ) { m_tries -= count; }
00099 uint32 tryCount() const { return m_tries; }
00100 bool isForIn() const { return m_ForinLoop != 0; }
00101 const StmtForin* ForinLoop() const { return m_ForinLoop; }
00102 void setForIn( const StmtForin* forin ) { m_ForinLoop = forin; }
00103 void setForLast( bool l ) { m_bIsForLast = l; }
00104 bool isForLast() const { return m_bIsForLast; }
00105 };
00106
00107 class c_varpar {
00108 public:
00109
00110 t_paramType m_type;
00111 union {
00112 const Value *value;
00113 const VarDef *vd;
00114 const Symbol *sym;
00115 int32 immediate;
00116 int64 immediate64;
00117 } m_content;
00118
00119 c_varpar():
00120 m_type( e_parND )
00121 {}
00122
00123 c_varpar( t_paramType t ):
00124 m_type( t )
00125 {}
00126
00127 explicit c_varpar( bool val ):
00128 m_type( val ? e_parTRUE : e_parFALSE )
00129 {}
00130
00131 c_varpar( const Value *val ):
00132 m_type( e_parVAL )
00133 {
00134 if ( val->isNil() )
00135 m_type = e_parNIL;
00136 else
00137 m_content.value = val;
00138 }
00139
00140 c_varpar( const VarDef *vd ):
00141 m_type( e_parVAR )
00142 {
00143 if ( vd->isNil() )
00144 m_type = e_parNIL;
00145 else
00146 m_content.vd = vd;
00147 }
00148
00149 c_varpar( const Symbol *sym ):
00150 m_type( e_parSYM )
00151 {
00152 m_content.sym = sym;
00153 }
00154
00155 c_varpar( const int32 immediate ):
00156 m_type( e_parINT32 )
00157 {
00158 m_content.immediate = immediate;
00159 }
00160
00161 c_varpar( const c_varpar &other ):
00162 m_type( other.m_type ),
00163 m_content( other.m_content )
00164 {}
00165
00166 void generate( GenCode *owner ) const;
00167
00168 };
00169
00170 friend class c_varpar;
00171
00172 c_varpar c_param_fixed( uint32 num ) {
00173 c_varpar ret( e_parNTD32 );
00174 ret.m_content.immediate = num;
00175 return ret;
00176 }
00177
00178 c_varpar c_param_str( uint32 num ) {
00179 c_varpar ret( e_parSTRID );
00180 ret.m_content.immediate = num;
00181 return ret;
00182 }
00183
00184 void gen_pcode( byte pcode )
00185 {
00186 gen_pcode( pcode, e_parND, e_parND, e_parND );
00187 }
00188
00189 void gen_pcode( byte pcode, const c_varpar &first )
00190 {
00191 gen_pcode( pcode, first, e_parND, e_parND );
00192 }
00193
00194 void gen_pcode( byte pcode, const c_varpar &first, const c_varpar &second )
00195 {
00196 gen_pcode( pcode, first, second, e_parND );
00197 }
00198
00199 void gen_pcode( byte pcode, const c_varpar &first, const c_varpar &second, const c_varpar &third );
00200 byte gen_pdef( const c_varpar &elem );
00201 void gen_var( const VarDef &def );
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00222 void pop_label();
00223
00224 void gen_function( const StmtFunction *func );
00225 void gen_block( const StatementList *slist );
00226 void gen_statement( const Statement *stmt );
00227 void gen_condition( const Value *stmt, int mode=-1 );
00228 void gen_value( const Value *stmt );
00229
00230 typedef enum {
00231 l_value,
00232 p_value,
00233 v_value
00234 } t_valType;
00235
00290 void gen_complex_value( const Value *value, t_valType &x_value );
00291
00297 void gen_complex_value( const Value *value ) { t_valType dummy; gen_complex_value( value, dummy ); }
00298
00305 void gen_expression( const Expression *expr, t_valType &x_value );
00306
00310 void gen_expression( const Expression *expr ) { t_valType dummy; gen_expression( expr, dummy ); }
00311
00312 void gen_dict_decl( const DictDecl *stmt );
00313 void gen_array_decl( const ArrayDecl *stmt );
00314 void gen_range_decl( const RangeDecl *stmt );
00321 void gen_push( const Value *val );
00328 void gen_load( const Value *target, const Value *source );
00329 void gen_store_to_deep( byte type, const Value *source, const Value *first, const Value *second );
00330
00331 void gen_inc_prefix( const Value *target );
00332 void gen_dec_prefix( const Value *target );
00333 void gen_inc_postfix( const Value *target );
00334 void gen_dec_postfix( const Value *target );
00335 void gen_autoassign( byte opcode, const Value *target, const Value *source );
00336 void gen_store_to_deep_A( byte type, const Value *first, const Value *second );
00337 void gen_store_to_deep_reg( byte type, const Value *first, const Value *second, t_paramType reg );
00338 void gen_load_from_deep( byte type, const Value *first, const Value *second );
00339 void gen_load_from_A( const Value *target );
00340 void gen_load_from_reg( const Value *target, t_paramType reg );
00341 int gen_refArray( const ArrayDecl *tarr, bool bGenArray );
00342 void gen_operand( const Value *stmt );
00343
00345 void gen_funcall( const Expression *call, bool fork=false );
00346 List m_labels;
00347 List m_labels_loop;
00348
00350 List m_functions;
00351
00352 uint32 m_pc;
00353 StringStream *m_outTemp;
00354 Module *m_module;
00355
00356 public:
00357 GenCode( Module *mod );
00358 virtual ~GenCode();
00359
00360 virtual void generate( const SourceTree *st );
00361 };
00362
00363 }
00364 #endif
00365
00366