00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef clone_INCLUDED
00042 #define clone_INCLUDED
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <algorithm>
00071
00072 #include <ext/hash_map>
00073
00074 #ifndef CXX_MEMORY_INCLUDED
00075 #include "cxx_memory.h"
00076 #endif
00077
00078 #include "defs.h"
00079
00080 #include "symtab.h"
00081 #include "wn.h"
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 class IPO_ADDR_HASH {
00115
00116 private:
00117
00118 static const INT hash_size = 256;
00119
00120 struct hash_node {
00121 void *orig;
00122 void *copy;
00123 struct hash_node *next;
00124 } *table[hash_size];
00125
00126 MEM_POOL *mem;
00127
00128 BOOL table_empty;
00129
00130 INT hash (INT key) {
00131 return (key & 0xff) ^ ((key >> 8) & 0xff);
00132 };
00133
00134 public:
00135
00136 IPO_ADDR_HASH (MEM_POOL *m) {
00137 mem = m;
00138 table_empty = TRUE;
00139 BZERO (table, sizeof(table));
00140 };
00141
00142 ~IPO_ADDR_HASH () {
00143 for (int i = 0; i < hash_size; i++){
00144 struct hash_node *cur = table[i];
00145 while(cur){
00146 struct hash_node *tmp = cur;
00147 cur = cur->next;
00148 CXX_DELETE(tmp, mem);
00149 }
00150 }
00151 }
00152
00153 void Insert (void *orig, void *copy);
00154
00155 void *Lookup (void *);
00156
00157 void Clear (void) {
00158 if (!table_empty) {
00159 BZERO (table, sizeof(table));
00160 table_empty = TRUE;
00161 }
00162 };
00163
00164 void Reset_Lookup (void *key);
00165 };
00166
00167
00168 class IPO_SYMTAB {
00169
00170 private:
00171
00172 SCOPE *_orig_scope_tab;
00173 SCOPE *_cloned_scope_tab;
00174 SYMTAB_IDX _orig_level;
00175 SYMTAB_IDX _cloned_level;
00176
00177 MEM_POOL *_mem;
00178 IPO_ADDR_HASH *_hash_maps;
00179
00180
00181 BOOL _same_file;
00182 UINT _cloned_st_last_idx;
00183
00184
00185
00186
00187
00188 UINT _cloned_inito_last_idx;
00189
00190
00191
00192
00193
00194 UINT _cloned_label_last_idx;
00195
00196
00197
00198
00199
00200 UINT _cloned_preg_last_idx;
00201
00202
00203
00204
00205 UINT _cloned_st_attr_last_idx;
00206
00207
00208
00209
00210 BOOL _is_new_clone;
00211
00212
00213
00214 void Copy_Local_Tables(BOOL);
00215
00216 template <class T>
00217 struct fix_table_entry
00218 {
00219 IPO_SYMTAB *_sym;
00220
00221 fix_table_entry(IPO_SYMTAB *sym): _sym(sym) {}
00222
00223 void operator () (UINT idx, T *entry) const;
00224 };
00225
00226 template <class T>
00227 struct promote_entry
00228 {
00229 IPO_SYMTAB *_sym;
00230
00231 promote_entry(IPO_SYMTAB *sym): _sym(sym) {}
00232
00233 void operator () (UINT idx, T* entry) const;
00234 };
00235
00236 template <class T>
00237 struct fix_base
00238 {
00239 IPO_SYMTAB *_sym;
00240
00241 fix_base(IPO_SYMTAB *sym): _sym(sym) {}
00242
00243 void operator () (UINT idx, T* entry) const;
00244 };
00245
00246 public:
00247
00248
00249
00250
00251 IPO_SYMTAB (SCOPE *orig_scope_tab,
00252 SYMTAB_IDX symtab_idx, MEM_POOL *m) :
00253 _orig_scope_tab(orig_scope_tab), _cloned_scope_tab(NULL), _mem(m),
00254 _same_file(TRUE), _orig_level(symtab_idx), _cloned_level(symtab_idx) {
00255 _cloned_st_last_idx = 0;
00256 _cloned_inito_last_idx = 0;
00257 _cloned_label_last_idx = 0;
00258 _cloned_preg_last_idx = 0;
00259 _cloned_st_attr_last_idx = 0;
00260 _is_new_clone = TRUE;
00261 _hash_maps = CXX_NEW (IPO_ADDR_HASH (m), m);
00262 };
00263
00264 ~IPO_SYMTAB (){
00265 CXX_DELETE(_hash_maps, Malloc_Mem_Pool);
00266 }
00267
00268
00269 IPO_SYMTAB (SCOPE *orig_scope_tab, SCOPE *cloned_scope_tab,
00270 SYMTAB_IDX orig_symtab_idx,
00271 SYMTAB_IDX cloned_symtab_idx, MEM_POOL *m,
00272 BOOL Same_file = TRUE) :
00273 _orig_scope_tab(orig_scope_tab), _cloned_scope_tab(cloned_scope_tab),
00274 _mem(m), _same_file(Same_file), _orig_level(orig_symtab_idx), _cloned_level(cloned_symtab_idx) {
00275 _cloned_st_last_idx = cloned_scope_tab[cloned_symtab_idx].st_tab->Size()-1;
00276 _cloned_inito_last_idx = cloned_scope_tab[cloned_symtab_idx].inito_tab->Size()-1;
00277 _cloned_label_last_idx = cloned_scope_tab[cloned_symtab_idx].label_tab->Size()-1;
00278 _cloned_preg_last_idx = cloned_scope_tab[cloned_symtab_idx].preg_tab->Size()-1;
00279 _cloned_st_attr_last_idx = cloned_scope_tab[cloned_symtab_idx].st_attr_tab->Size()-1;
00280 _is_new_clone = FALSE;
00281 _hash_maps = CXX_NEW (IPO_ADDR_HASH (m), m);
00282 };
00283
00284
00285 void New_Symtab ();
00286
00287
00288 void Update_Symtab (BOOL);
00289
00290
00291 void Promote_Statics ();
00292
00293 INITO_IDX Copy_INITO(INITO_IDX orig_init=0);
00294
00295
00296
00297 INITV_IDX Clone_INITVs_For_EH (INITV_IDX , INITO_IDX );
00298
00299
00300
00301 SCOPE* Get_orig_scope_tab () const { return _orig_scope_tab; }
00302 SCOPE* Get_cloned_scope_tab () const { return _cloned_scope_tab; }
00303 SYMTAB_IDX Get_orig_level () const { return _orig_level;}
00304 SYMTAB_IDX Get_cloned_level () const { return _cloned_level;}
00305 UINT Get_cloned_st_last_idx () const { return _cloned_st_last_idx;}
00306 UINT Get_cloned_inito_last_idx() const { return _cloned_inito_last_idx;}
00307 void Set_cloned_inito_last_idx (UINT idx) { _cloned_inito_last_idx = idx;}
00308 UINT Get_cloned_label_last_idx () const { return _cloned_label_last_idx;}
00309 void Set_cloned_label_last_idx (UINT idx) { _cloned_label_last_idx = idx;}
00310 UINT Get_cloned_preg_last_idx() const { return _cloned_preg_last_idx;}
00311 UINT Get_cloned_st_attr_last_idx() const { return _cloned_st_attr_last_idx;}
00312 BOOL Is_new_clone () const { return _is_new_clone; }
00313 BOOL Same_file () const { return _same_file; }
00314
00315 void Set_Cloned_Symtab (SCOPE *scope_tab) {
00316 _cloned_scope_tab = scope_tab;
00317 }
00318
00319 void Set_Cloned_ST(ST* old_st, ST* new_st);
00320
00321 ST* Get_Cloned_ST(ST* old_st) {return (ST *) _hash_maps->Lookup (old_st); };
00322
00323 void Hide_Cloned_ST (ST *st) { _hash_maps->Reset_Lookup (st); };
00324
00325 void Set_Cloned_INITO(INITO* old_inito, INITO_IDX new_inito);
00326
00327 INITO_IDX Get_Cloned_INITO_IDX(INITO* old_inito) {
00328 return (INITO_IDX)(INTPTR)_hash_maps->Lookup (old_inito);
00329 }
00330
00331 ST* Get_ST(ST* old_st) {
00332 return ((ST_level(old_st) != _orig_level)? old_st : &_cloned_scope_tab[_cloned_level].st_tab->Entry(ST_index(old_st)+_cloned_st_last_idx));
00333 }
00334
00335 INITO_IDX Get_INITO_IDX(INITO_IDX old_inito) {
00336 return make_INITO_IDX(INITO_IDX_index(old_inito)+_cloned_inito_last_idx, _cloned_level);
00337 }
00338
00339 INITO* Get_INITO(INITO_IDX idx) {
00340 SYMTAB_IDX level = INITO_IDX_level (idx);
00341 UINT32 index = INITO_IDX_index (idx);
00342 return (&_orig_scope_tab[_orig_level].inito_tab->Entry(index));
00343 }
00344
00345 ST* Get_Orig_ST(ST_IDX idx) {
00346 return (&_orig_scope_tab[ST_IDX_level(idx)].st_tab->Entry(ST_IDX_index(idx)));
00347 }
00348
00349 ST* IPO_Copy_ST (ST* st, SYMTAB_IDX scope);
00350
00351 BOOL Is_Cloned_ST(ST* st) { return ((ST_level(st) != _cloned_level) ||
00352 (ST_index(st) > _cloned_st_last_idx)); }
00353
00354 };
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 class IPO_CLONE {
00368
00369 private:
00370
00371 WN *_orig_pu;
00372 WN *_cloned_pu;
00373
00374 IPO_SYMTAB *_sym;
00375
00376 static INT _label;
00377 static const INT _default_buf_size = 2048;
00378
00379 WN_MAP_TAB *_orig_map_tab;
00380 WN_MAP_TAB *_cloned_map_tab;
00381
00382 WN_MAP _parent_map;
00383
00384 MEM_POOL *_mem;
00385
00386 BOOL _same_file;
00387
00388 mUINT16 _cloned_node_file_id;
00389
00390
00391 WN *_raw_buffer;
00392 UINT _raw_buf_size;
00393
00394
00395
00396
00397
00398 void Fix_ST (WN *, WN *);
00399 void Fix_TY (WN *, WN *);
00400 void Fix_INITO(WN* cloned_wn, WN* wn);
00401
00402 WN *Copy_Node (const WN *src_wn);
00403
00404 public:
00405
00406
00407
00408
00409
00410 IPO_CLONE (WN *pu,
00411 SCOPE *scope_tab,
00412 SYMTAB_IDX symtab_idx,
00413 WN_MAP_TAB* map_tab,
00414 MEM_POOL *map_pool,
00415 MEM_POOL *m) :
00416 _orig_map_tab(map_tab), _cloned_map_tab(NULL), _parent_map(0),
00417 _sym(CXX_NEW (IPO_SYMTAB (scope_tab, symtab_idx, m), m)),
00418 _orig_pu(pu), _cloned_pu(NULL),
00419 _mem(map_pool),
00420 _cloned_node_file_id(0),
00421 _same_file(TRUE), _raw_buf_size(0)
00422 {
00423 };
00424
00425
00426
00427
00428
00429
00430 IPO_CLONE (WN_MAP_TAB *caller,
00431 WN_MAP_TAB *callee,
00432 WN_MAP parent,
00433 SCOPE *callee_scope_tab=NULL,
00434 SCOPE *caller_scope_tab=NULL,
00435 SYMTAB_IDX callee_symtab_idx=0,
00436 SYMTAB_IDX caller_symtab_idx=0,
00437 IPO_SYMTAB *cloned_symtab=NULL,
00438 MEM_POOL *map_pool=NULL,
00439 BOOL same_file=TRUE,
00440 mUINT16 filenum=0) :
00441 _orig_pu(NULL), _cloned_pu(NULL),
00442 #if (!((defined(linux) || defined(BUILD_OS_DARWIN)) && defined(_LP64)))
00443 _sym(cloned_symtab? cloned_symtab :
00444 (callee_scope_tab == NULL? NULL :
00445 CXX_NEW(IPO_SYMTAB(callee_scope_tab,
00446 caller_scope_tab,
00447 callee_symtab_idx,
00448 caller_symtab_idx,
00449 Malloc_Mem_Pool, same_file),
00450 Malloc_Mem_Pool))),
00451 #endif
00452 _orig_map_tab (callee), _cloned_map_tab(caller), _parent_map(parent),
00453 _mem(map_pool),
00454 _cloned_node_file_id(filenum),
00455 _same_file(same_file), _raw_buf_size (0)
00456 {
00457 #if (defined(linux) || defined(BUILD_OS_DARWIN)) && defined(_LP64)
00458 if ((cloned_symtab == NULL) && (callee_scope_tab != NULL))
00459 _sym = CXX_NEW (IPO_SYMTAB (callee_scope_tab,
00460 caller_scope_tab,
00461 callee_symtab_idx,
00462 caller_symtab_idx,
00463 Malloc_Mem_Pool, same_file),
00464 Malloc_Mem_Pool);
00465 else
00466 _sym = cloned_symtab;
00467 #endif // (!((defined(linux) || defined(BUILD_OS_DARWIN)) && defined(_LP64)))
00468 };
00469
00470
00471 IPO_CLONE (IPO_SYMTAB *ipo_symtab) :
00472 _orig_map_tab(NULL), _cloned_map_tab(NULL), _parent_map(0),
00473 _sym(ipo_symtab),
00474 _orig_pu(NULL), _cloned_pu(NULL),
00475 _mem(NULL),
00476 _cloned_node_file_id(0),
00477 _same_file(ipo_symtab->Same_file()), _raw_buf_size(0)
00478 {
00479 };
00480
00481
00482 void Set_Entry_Point (WN *, WN *, ST *);
00483 #if 0
00484 void Set_Entry_Point (WN *, WN *);
00485 #endif
00486
00487
00488 WN *Clone_Tree (WN *, ST *clone_st = NULL);
00489
00490
00491 void New_Clone (ST *clone_st = NULL);
00492
00493
00494 void Promote_Statics() { _sym ->Promote_Statics();}
00495
00496
00497 WN *Get_Cloned_PU () const { return _cloned_pu; };
00498
00499 SCOPE* Get_Cloned_Symtab () const { return _sym->Get_cloned_scope_tab (); };
00500
00501 char *Get_Func_Name () { return ST_name(WN_st(_cloned_pu)); };
00502
00503 WN_MAP_TAB *Get_Cloned_maptab () { return _cloned_map_tab; };
00504
00505 WN_MAP Get_parent_map () { return _parent_map; };
00506
00507 ST *Get_Func_ST () { return WN_st(_cloned_pu); };
00508
00509 IPO_SYMTAB *Get_sym() { return _sym; };
00510
00511
00512 };
00513
00514
00515 #endif // clone_INCLUDED
00516