2015-09-07 3 views
0

Вот это простая программа C:Определить смещение байта из GetElementPtr

struct S {char c; short arr[16]; char dummy2;}; 

extern struct S A[20]; 
extern short* p; 

int main() { 
    p = &A[10].arr[6]; 
    return 0; 
} 

А вот LLVM IR:

%struct.S = type { i8, [16 x i16], i8 } 

@A = external global [20 x %struct.S] 
@p = external global i16* 

; Function Attrs: nounwind 
define i32 @main() #0 { 
entry: 
    store i16* getelementptr inbounds ([20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6), i16** @p, align 8, !tbaa !1 
    ret i32 0 
} 

Как я могу вычислить смещение байта, который добавляется к @A по getelementptr?

я могу перебрать и распечатать GEP достаточно легко:

auto& P = *GEP.getPointerOperand(); 
    Out << "GEP("; 
    GEP.getType()->print(Out); // return type 
    Out << ", "; 
    P.printAsOperand(Out); // base 
    for (auto i=0U; i<GEP.getNumIndices(); i++) { 
    Out << ", "; 
    GEP.getOperand(i+1)->printAsOperand(Out); // index i 
    } 
    Out << ")\n"; 

Печатается:

GEP(i16*, [20 x %struct.S]* @A, i64 0, i64 10, i32 1, i64 6) 

Допуская все индексы постоянны целое, как можно определить смещение в байтах относительно базового указателя?

ответ

1

Можете ли вы использовать инструкции ptrtoint и вычесть их?