2013-07-31 80 views
0

鐺使用以下方案來定義LLVM IR一個具體類型:()例如虛擬INT F({})段故障

%"mytype" = type {virtual_method_types, field_types, super_types} 

並且爲了調用虛方法以下方案使用:

%0 = load %"mytype"** %this 
%1 = bitcast %"mytype"* %0 to i32 (%"mytype"*)*** 
%vtable = load i32 (%"mytype"*)*** %1 
%method = getelementptr inbounds i32 (%"mytype"*)** %vtable, i64 0 (index of f() in vt) 
%ld = load i32 (%"mytype"*)** %method 
%call = call i32 (%"mytype"*)* %ld (%"mytype"* %0) 

但是,如果使用下面的方案,而不是在上面的代碼中應該改變以防止seg故障?

%"mytype" = type {field_types, super_types, virtual_method_types} 

回答

0

此行得到指針指向的對象,這是vptr的(指向虛表)的開頭:

%1 = bitcast %"mytype"* %0 to i32 (%"mytype"*)*** 

如果放置的vptr在對象的末尾,而不是,這段代碼需要更改爲使用 %0以獲得指向vptr的正確位置的指針(或者getelementptr%this上,但是無論如何您需要加載%this,它並沒有真正的幫助)。例如,如果vptr將是%"mytype"中的第12個字段,則需要類似如下內容:

%1 = extractvalue %"mytype" %0, 12