AsmParser fails

Hello everybody,

unfortunately, I have some problems using the LLVM assembler parser ...

- use llvm-gcc ( llvm-gcc -O0 -S --emit-llvm -o system2.ll system2.c)
to translate the following small example into LLVM assembly language:

int common_func1() {
  return 5;
}

int common_func2(int a) {
  return a + 5;
}

void Handler1() {
  int e = 4;
  int f = common_func1();
  int ret = common_func2(e);
}

void Handler2() {
  int ret = common_func2(common_func1() + 3 - 2 * 5);
}

- this yields the following LLVM assembly listing:

; ModuleID = 'system2.c'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64"
target triple = "i686-pc-linux-gnu"

define i32 @common_func1() {
entry:
  %retval = alloca i32, align 4 ; <i32*> [#uses=2]
  %tmp = alloca i32, align 4 ; <i32*> [#uses=2]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  store i32 5, i32* %tmp
  %tmp1 = load i32* %tmp ; <i32> [#uses=1]
  store i32 %tmp1, i32* %retval
  br label %return

return: ; preds = %entry
  %retval2 = load i32* %retval ; <i32> [#uses=1]
  ret i32 %retval2
}

define i32 @common_func2(i32 %a) {
entry:
  %a_addr = alloca i32 ; <i32*> [#uses=2]
  %retval = alloca i32, align 4 ; <i32*> [#uses=2]
  %tmp = alloca i32, align 4 ; <i32*> [#uses=2]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  store i32 %a, i32* %a_addr
  %tmp1 = load i32* %a_addr ; <i32> [#uses=1]
  %tmp2 = add i32 %tmp1, 5 ; <i32> [#uses=1]
  store i32 %tmp2, i32* %tmp
  %tmp3 = load i32* %tmp ; <i32> [#uses=1]
  store i32 %tmp3, i32* %retval
  br label %return

return: ; preds = %entry
  %retval4 = load i32* %retval ; <i32> [#uses=1]
  ret i32 %retval4
}

define void @Handler1() {
entry:
  %e = alloca i32, align 4 ; <i32*> [#uses=2]
  %f = alloca i32, align 4 ; <i32*> [#uses=1]
  %ret = alloca i32, align 4 ; <i32*> [#uses=1]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  store i32 4, i32* %e
  %tmp = call i32 @common_func1( ) ; <i32> [#uses=1]
  store i32 %tmp, i32* %f
  %tmp1 = load i32* %e ; <i32> [#uses=1]
  %tmp2 = call i32 @common_func2( i32 %tmp1 ) ; <i32> [#uses=1]
  store i32 %tmp2, i32* %ret
  br label %return

return: ; preds = %entry
  ret void
}

define void @Handler2() {
entry:
  %ret = alloca i32, align 4 ; <i32*> [#uses=1]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  %tmp = call i32 @common_func1( ) ; <i32> [#uses=1]
  %tmp1 = sub i32 %tmp, 7 ; <i32> [#uses=1]
  %tmp2 = call i32 @common_func2( i32 %tmp1 ) ; <i32> [#uses=1]
  store i32 %tmp2, i32* %ret
  br label %return

return: ; preds = %entry
  ret void
}

- when I now want to parse this file using the AsmParser via
llvm::ParseAssemblyFile my program crashes with the following error
message:

/usr/include/c++/4.1.2/debug/vector:192:error: attempt to subscript
    container with out-of-bounds index 0, but container only holds 0
    elements.

Objects involved in the operation:
sequence "this" @ 0x0xbf848814 {
  type = N15__gnu_debug_def6vectorIPN4llvm5ValueESaIS3_EEE;
}

when I try to use llvm-as to assemble the whole thing, I get the same
error message and a stack trace:

/usr/include/c++/4.1.2/debug/vector:192:error: attempt to subscript
    container with out-of-bounds index 0, but container only holds 0
    elements.

Objects involved in the operation:
sequence "this" @ 0x0xbfd32234 {
  type = N15__gnu_debug_def6vectorIPN4llvm5ValueESaIS3_EEE;
}
/home/scheler/llvm/install_debug/bin/llvm-as((anonymous
namespace)::PrintStackTrace()+0x1a)[0x83dbbba]
/home/scheler/llvm/install_debug/bin/llvm-as((anonymous
namespace)::SignalHandler(int)+0x112)[0x83dbe80]
[0xb7fda420]
/lib/libc.so.6(abort+0x101)[0xb7d5b801]
/usr/lib/libstdc++.so.6(__gnu_debug::_Error_formatter::_M_error()
const+0x1fe)[0xb7ed79ee]
/home/scheler/llvm/install_debug/bin/llvm-as(__gnu_debug_def::vector<llvm::Value*,
std::allocator<llvm::Value*> >::operator[](unsigned
int)+0xb4)[0x82a2698]
/home/scheler/llvm/install_debug/bin/llvm-as(llvmAsmparse()+0xd712)[0x8290bea]
/home/scheler/llvm/install_debug/bin/llvm-as[0x8292426]
/home/scheler/llvm/install_debug/bin/llvm-as(llvm::RunVMAsmParser(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&,
_IO_FILE*)+0x4a)[0x82928da]
/home/scheler/llvm/install_debug/bin/llvm-as(llvm::ParseAssemblyFile(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&,
llvm::ParseError*)+0xf3)[0x827f8c9]
/home/scheler/llvm/install_debug/bin/llvm-as(main+0x84)[0x8248656]
/lib/libc.so.6(__libc_start_main+0xdc)[0xb7d46f9c]
/home/scheler/llvm/install_debug/bin/llvm-as(__gxx_personality_v0+0x221)[0x8248211]

Am I missing something substantial? Is this a bug in the assembler
parser of the LLVM? Any help is appreciated!

Ciao, Fabian

Hi,

/usr/include/c++/4.1.2/debug/vector:192:error: attempt to subscript

don't use the debugging version of the STL: it applies a (too?) strict
check which LLVM code can fail. We are still trying to decide how to
deal with this.

Ciao,

Duncan.

I've got fixes for this problem (and others), but am still running tests to
make sure I've caught all of them.

                                               -Dave