Basic frontend framework: Segfault on append_to_statement_list

Rehno Lindeque
Mon Jul 16 16:39:00 GMT 2007

I'm struggling a lot to get a basic frontend up and running. It
segfaults on append_to_statement_list(...) when the function tries to
get the statement list's tail iterator (via STATEMENT_LIST_TAIL(...)).
 This is of course after I have called alloc_stmt_list(...).

Can anyone point to _anything_ at all I might be missing?

GCC base is 4.2.0. For reference, this is my basic parse function:

///////////////// START OF CODE /////////////////

tree build_function_decl (const char *name, bool external, tree function_type)
  tree function_decl;

  function_decl                   = build_fn_decl(name, function_type);
  DECL_EXTERNAL(function_decl)    = external;
  DECL_ARTIFICIAL(function_decl)  = false;
  TREE_STATIC(function_decl)      = !external;
  DECL_CONTEXT(function_decl)     = NULL_TREE;

  return function_decl;

void build_function(tree function_decl, tree stmt_list, tree block)
  tree result_decl;

  DECL_INITIAL(function_decl)     = block;
  DECL_SAVED_TREE(function_decl)  = build3(BIND_EXPR, void_type_node,
BLOCK_VARS(block), stmt_list, block);

  result_decl                 = build_decl(RESULT_DECL, NULL_TREE,
  DECL_CONTEXT(result_decl)   = function_decl;
  DECL_RESULT(function_decl)  = result_decl;


void experimental_parse_file(int debug)
  /* Build main function */

  const char* const message = "Welcome to Experimental!";

  tree welcome_string_literal;

  tree main_function_type;
  tree main_function_decl;
  tree main_function_block;
  tree main_function_stmt_list;

  tree puts_function_type;
  tree puts_function_decl;
  tree char_pointer;

  tree puts_function_call;
  tree puts_function_args;

  /* Build main function (and call puts (builtin??) function) */
  /* Build puts function declaration: int puts(char*); */
  char_pointer        = build_pointer_type(char_type_node);
  puts_function_type  = build_function_type_list(integer_type_node,
char_pointer, NULL_TREE);
  puts_function_decl  = build_function_decl("puts", true, puts_function_type);

  /* Build main function declaration: int main(); */
  main_function_type = build_function_type_list(integer_type_node, NULL_TREE);
  main_function_decl = build_function_decl("main", false, main_function_type);
  DECL_SOURCE_FILE(main_function_decl) = main_input_filename;
  DECL_SOURCE_LINE(main_function_decl) = 1;

  main_function_block     = build_block(NULL_TREE, NULL_TREE,
  main_function_stmt_list = alloc_stmt_list();

  welcome_string_literal  = build_string_literal(strlen(message) + 1, message);
  puts_function_args      = tree_cons(NULL_TREE,
welcome_string_literal, NULL_TREE);
  puts_function_call      =
build_function_call_expr(puts_function_decl, puts_function_args);

  /***     ERROR: SEGFAULT OCCURS HERE!     ***/
  append_to_statement_list(puts_function_call, &main_function_stmt_list);

  build_function(main_function_decl, main_function_stmt_list,

  /* Process trees */
  cgraph_finalize_function(main_function_decl, false);

///////////////// END OF CODE /////////////////

Kind regards,
Rehno Lindeque

More information about the Gcc mailing list