Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

ArrayType Class Reference

#include <pac.h>

Inheritance diagram for ArrayType:

Inheritance graph
[legend]
Collaboration diagram for ArrayType:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ArrayType (Type *arg_elemtype, Expr *arg_length)
 ArrayType (Type *arg_elemtype, SeqEnd *arg_end)
 ~ArrayType ()
const char * DataType ()
void Prepare (Env *env, const char *enclosing_lvalue)
void genPubDecls (Output *out, Env *env)
void genPrivDecls (Output *out, Env *env)
void genInitCode (Output *out, Env *env)
void genCleanUpCode (Output *out, Env *env)
void do_genParseCode (Output *out, Env *env, const DataPtr &data)
int StaticSize (Env *env) const
void SetBoundaryChecked ()
bool ByteOrderSensitive () const

Protected Member Functions

void init ()
void genDynamicSize (Output *out, Env *env, const DataPtr &data)
void genArrayLength (Output *out_cc, Env *env)

Protected Attributes

Typeelemtype
Exprlength
SeqEndend
char * data_type
IDarraylength_var
IDelem_var
IDelem_data_var

Constructor & Destructor Documentation

ArrayType::ArrayType Type arg_elemtype,
Expr arg_length
 

Definition at line 898 of file pac.cc.

References init().

00899         : elemtype(arg_elemtype), length(arg_length), end(0)
00900         {
00901         init();
00902         // data_type = nfmt("%s *", elemtype->DataType());
00903         }

ArrayType::ArrayType Type arg_elemtype,
SeqEnd arg_end
 

Definition at line 905 of file pac.cc.

References init().

00906         : elemtype(arg_elemtype), length(0), end(arg_end)
00907         {
00908         init();
00909         }

ArrayType::~ArrayType  ) 
 

Definition at line 920 of file pac.cc.

References arraylength_var, data_type, elem_data_var, and elem_var.

00921         {
00922         delete elem_var;
00923         delete elem_data_var;
00924         delete data_type;
00925         delete arraylength_var;
00926         }


Member Function Documentation

bool ArrayType::ByteOrderSensitive  )  const [inline, virtual]
 

Implements Type.

Definition at line 496 of file pac.h.

References Type::ByteOrderSensitive(), and elemtype.

00497                 { 
00498                 return elemtype->ByteOrderSensitive(); 
00499                 }

const char * ArrayType::DataType  )  [virtual]
 

Implements Type.

Definition at line 928 of file pac.cc.

References data_type.

00929         {
00930         return data_type;
00931         }

void ArrayType::do_genParseCode Output out,
Env env,
const DataPtr data
[virtual]
 

Implements Type.

Definition at line 997 of file pac.cc.

References Type::AddSizeVar(), arraylength_var, CONST, Type::DataSize(), Type::DataType(), Output::dec_indent(), elem_data_var, elem_var, elemtype, end, Env::Evaluated(), genArrayLength(), Type::genBoundaryCheck(), Type::genParseCode(), SeqEnd::genParseCode(), Output::inc_indent(), Env::LValue(), ID::Name(), nfmt(), Output::println(), DataPtr::PtrExpr(), Env::RValue(), Env::SetEvaluated(), Type::SetValueVar(), Type::StaticSize(), and StaticSize().

00999         {
01000         bool compute_size_var;
01001 
01002         if ( ! end && 
01003              ( StaticSize(env) >= 0 || elemtype->StaticSize(env) >= 0 ) )
01004                 {
01005                 compute_size_var = false;
01006                 genBoundaryCheck(out_cc, env, data);
01007                 }
01008         else
01009                 {
01010                 AddSizeVar(out_cc, env);
01011                 compute_size_var = (size_var && ! env->Evaluated(size_var));
01012                 }
01013 
01014         genArrayLength(out_cc, env);
01015         char* array_str;
01016         bool use_vector;
01017         if ( env->Evaluated(arraylength_var) )
01018                 {
01019                 out_cc->println("make_array<%s>(%s, %s);",
01020                         elemtype->DataType(), 
01021                         lvalue,
01022                         env->RValue(arraylength_var));
01023                 array_str = nfmt("%s.v", lvalue);
01024                 use_vector = false;
01025                 }
01026         else
01027                 {
01028                 array_str = nfmt("t_%s__vec", value_var->Name());
01029                 out_cc->println("std::vector<%s> %s;", 
01030                         elemtype->DataType(),
01031                         array_str);
01032                 use_vector = true;
01033                 }
01034 
01035         char* elem_it = nfmt("t_%s__it", elem_var->Name());
01036 
01037         out_cc->println("const uint8* %s = %s;", 
01038                 env->LValue(elem_data_var), data.PtrExpr());
01039         env->SetEvaluated(elem_data_var);
01040 
01041         DataPtr elem_data(env, elem_data_var, 0);
01042 
01043         out_cc->println("for ( int %s = 0; ", elem_it);
01044         if ( env->Evaluated(arraylength_var) )
01045                 out_cc->println("       %s < %s; ", elem_it, env->RValue(arraylength_var));
01046         else
01047                 out_cc->println("       /* forever */; ");
01048         out_cc->println("       ++%s )", elem_it);
01049         out_cc->inc_indent();
01050         out_cc->println("{");
01051         if ( end )
01052                 end->genParseCode(out_cc, env, elem_data_var, value_var->Name());
01053         out_cc->println("%s %s;", 
01054                 elemtype->DataType(), env->LValue(elem_var));
01055 
01056         elemtype->genParseCode(out_cc, env, elem_data);
01057 
01058         // Assign the element
01059         if ( ! use_vector )
01060                 out_cc->println("%s[%s] = %s;", 
01061                         array_str, elem_it, env->RValue(elem_var));
01062         else
01063                 out_cc->println("%s.push_back(%s);", 
01064                         array_str, env->RValue(elem_var));
01065 
01066         out_cc->println("%s += %s;", 
01067                 env->LValue(elem_data_var), elemtype->DataSize(0, env, elem_data));
01068         out_cc->println("}");
01069         out_cc->dec_indent();
01070 
01071         if ( use_vector )
01072                 {
01073                 out_cc->println("make_array<%s>(%s, %s.size());",
01074                         elemtype->DataType(), 
01075                         lvalue,
01076                         array_str);
01077                 out_cc->println("for ( int %s = 0; %s < %s.length; ++%s )", 
01078                         elem_it, elem_it, env->RValue(value_var), elem_it);
01079                 out_cc->inc_indent();
01080                 out_cc->println("%s[%s] = %s[%s];", 
01081                         lvalue, elem_it,
01082                         array_str, elem_it);
01083                 out_cc->dec_indent();
01084                 }
01085 
01086         if ( compute_size_var )
01087                 {
01088                 // Compute the data size
01089                 out_cc->println("int %s = %s - %s;", 
01090                         env->LValue(size_var),
01091                         env->RValue(elem_data_var),
01092                         data.PtrExpr());
01093                 env->SetEvaluated(size_var);
01094                 }
01095 
01096         delete [] array_str;
01097         delete [] elem_it;
01098 
01099         elemtype->SetValueVar(0, CONST);
01100         }

void ArrayType::genArrayLength Output out_cc,
Env env
[protected]
 

Definition at line 948 of file pac.cc.

References arraylength_var, Expr::EvalExpr(), Env::Evaluated(), length, Env::LValue(), Output::println(), and Env::SetEvaluated().

Referenced by do_genParseCode(), and genDynamicSize().

00949         {
00950         if ( ! length )
00951                 return;
00952 
00953         if ( env->Evaluated(arraylength_var) )
00954                 return;
00955 
00956         out_cc->println("int %s = %s;", 
00957                 env->LValue(arraylength_var), length->EvalExpr(out_cc, env));
00958 
00959         env->SetEvaluated(arraylength_var);
00960         }

void ArrayType::genCleanUpCode Output out,
Env env
[virtual]
 

Reimplemented from Type.

Definition at line 978 of file pac.cc.

References Type::DataType(), Output::dec_indent(), elem_var, elemtype, Type::genCleanUpCode(), Output::inc_indent(), Env::LValue(), Type::needsCleanUp(), Output::println(), and Env::RValue().

00979         {
00980         if ( elemtype->needsCleanUp() )
00981                 {
00982                 out_cc->println("for ( int i = 0; i < %s.size(); ++i )", 
00983                         env->RValue(value_var));
00984                 out_cc->inc_indent();
00985                 out_cc->println("{");
00986                 out_cc->println("%s %s = %s[i];", 
00987                         elemtype->DataType(), 
00988                         env->LValue(elem_var), 
00989                         lvalue);
00990                 elemtype->genCleanUpCode(out_cc, env);
00991                 out_cc->println("}");
00992                 out_cc->dec_indent();
00993                 }
00994         out_cc->println("delete_array(%s);", lvalue);
00995         }

void ArrayType::genDynamicSize Output out,
Env env,
const DataPtr data
[protected, virtual]
 

Implements Type.

Definition at line 1102 of file pac.cc.

References Type::AddSizeVar(), arraylength_var, ASSERT, DEBUG_MSG, elemtype, end, Env::Evaluated(), genArrayLength(), Type::genParseCode(), length, Env::LValue(), ID::Name(), Output::println(), Env::RValue(), Env::SetEvaluated(), and Type::StaticSize().

01104         {
01105         DEBUG_MSG("Generating dynamic size for array `%s'\n", 
01106                 value_var->Name());
01107 
01108         int elem_w = elemtype->StaticSize(env);
01109         if ( ! end && length && elem_w >= 0 )
01110                 {
01111                 // If the elements have a fixed size,
01112                 // we can get away by only dynamically computing the array length
01113                 AddSizeVar(out_cc, env);
01114                 bool compute_size_var = (size_var && ! env->Evaluated(size_var));
01115                 ASSERT(compute_size_var);
01116                 genArrayLength(out_cc, env);
01117                 ASSERT(env->Evaluated(arraylength_var));
01118                 out_cc->println("int %s = %d * %s;",
01119                         env->LValue(size_var), elem_w, env->RValue(arraylength_var));
01120                 env->SetEvaluated(size_var);
01121                 return;
01122                 }
01123         else
01124                 // Otherwise we need parse the array dynamically
01125                 genParseCode(out_cc, env, data);
01126         }

void ArrayType::genInitCode Output out,
Env env
[virtual]
 

Reimplemented from Type.

Definition at line 972 of file pac.cc.

References Output::println().

00973         {
00974         out_cc->println("make_array(%s, 0);", lvalue);
00975         // Type::genInitCode(out_cc, env);
00976         }

void ArrayType::genPrivDecls Output out,
Env env
[virtual]
 

Reimplemented from Type.

Definition at line 967 of file pac.cc.

References Type::genPrivDecls().

00968         {
00969         Type::genPrivDecls(out_h, env);
00970         }

void ArrayType::genPubDecls Output out,
Env env
[virtual]
 

Reimplemented from Type.

Definition at line 962 of file pac.cc.

References Type::genPubDecls().

00963         {
00964         Type::genPubDecls(out_h, env);
00965         }

void ArrayType::init  )  [protected]
 

Definition at line 911 of file pac.cc.

References arraylength_var, data_type, Type::DataType(), elem_data_var, elem_var, elemtype, and nfmt().

Referenced by ArrayType().

00912         {
00913         tot_ = ARRAY;
00914         arraylength_var = 0;
00915         elem_var = 0;
00916         elem_data_var = 0;
00917         data_type = nfmt("array<%s>", elemtype->DataType());
00918         }

void ArrayType::Prepare Env env,
const char *  enclosing_lvalue
[virtual]
 

Reimplemented from Type.

Definition at line 933 of file pac.cc.

References Env::AddID(), arraylength_var, elem_data_var, elem_var, elemtype, fmt(), ID, ID::Name(), Type::Prepare(), Type::SetValueVar(), and TEMP_VAR.

00934         {
00935         Type::Prepare(env, enclosing_lvalue);
00936 
00937         arraylength_var = new ID(fmt("%s__arraylength", value_var->Name()));
00938         env->AddID(arraylength_var, TEMP_VAR);
00939 
00940         elem_var = new ID(fmt("%s__elem", value_var->Name()));
00941         elemtype->SetValueVar(elem_var, TEMP_VAR);
00942         elemtype->Prepare(env, 0);
00943 
00944         elem_data_var = new ID(fmt("%s__data", elem_var->Name()));
00945         env->AddID(elem_data_var, TEMP_VAR);
00946         }

void ArrayType::SetBoundaryChecked  )  [virtual]
 

Reimplemented from Type.

Definition at line 1148 of file pac.cc.

References elemtype, and Type::SetBoundaryChecked().

01149         {
01150         Type::SetBoundaryChecked();
01151         elemtype->SetBoundaryChecked();
01152         }

int ArrayType::StaticSize Env env  )  const [virtual]
 

Implements Type.

Definition at line 1128 of file pac.cc.

References Expr::ConstFold(), DEBUG_MSG, elemtype, end, length, ID::Name(), and Type::StaticSize().

Referenced by do_genParseCode().

01129         {
01130         int num = 0;
01131 
01132         if ( end )
01133                 return -1;
01134 
01135         if ( ! length || ! length->ConstFold(env, num) )
01136                 return -1;
01137 
01138         int elem_w = elemtype->StaticSize(env);
01139         if ( elem_w < 0 )
01140                 return -1;
01141 
01142         DEBUG_MSG("static size of %s:%s = %d * %d\n", 
01143                 decl_id->Name(), lvalue, elem_w, num);
01144 
01145         return num * elem_w;
01146         }


Member Data Documentation

ID* ArrayType::arraylength_var [protected]
 

Definition at line 511 of file pac.h.

Referenced by do_genParseCode(), genArrayLength(), genDynamicSize(), init(), Prepare(), and ~ArrayType().

char* ArrayType::data_type [protected]
 

Definition at line 510 of file pac.h.

Referenced by DataType(), init(), and ~ArrayType().

ID* ArrayType::elem_data_var [protected]
 

Definition at line 513 of file pac.h.

Referenced by do_genParseCode(), init(), Prepare(), and ~ArrayType().

ID* ArrayType::elem_var [protected]
 

Definition at line 512 of file pac.h.

Referenced by do_genParseCode(), genCleanUpCode(), init(), Prepare(), and ~ArrayType().

Type* ArrayType::elemtype [protected]
 

Definition at line 506 of file pac.h.

Referenced by ByteOrderSensitive(), do_genParseCode(), genCleanUpCode(), genDynamicSize(), init(), Prepare(), SetBoundaryChecked(), and StaticSize().

SeqEnd* ArrayType::end [protected]
 

Definition at line 508 of file pac.h.

Referenced by do_genParseCode(), genDynamicSize(), and StaticSize().

Expr* ArrayType::length [protected]
 

Definition at line 507 of file pac.h.

Referenced by genArrayLength(), genDynamicSize(), and StaticSize().


The documentation for this class was generated from the following files:
Generated on Wed Sep 14 03:07:39 2005 for bro_docs by doxygen 1.3.5