drgn/libdrgn/lexer.c
Omar Sandoval 75c3679147 Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:

- It's too slow for some common use cases, like iterating over large
  data structures.
- It can't be reused in utilities written in other languages.

This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:

- Types are now represented by a single Type class rather than the messy
  polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
  functions.

The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.

Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-04-02 14:12:07 -07:00

60 lines
1.3 KiB
C

// Copyright 2018-2019 - Omar Sandoval
// SPDX-License-Identifier: GPL-3.0+
#include "internal.h"
#include "lexer.h"
void drgn_lexer_init(struct drgn_lexer *lexer, drgn_lexer_func func,
const char *str)
{
lexer->func = func;
lexer->p = str;
lexer->stack = NULL;
lexer->stack_len = 0;
lexer->stack_capacity = 0;
}
void drgn_lexer_deinit(struct drgn_lexer *lexer)
{
free(lexer->stack);
}
struct drgn_error *drgn_lexer_pop(struct drgn_lexer *lexer,
struct drgn_token *token)
{
if (lexer->stack_len) {
*token = lexer->stack[--lexer->stack_len];
return NULL;
}
return lexer->func(lexer, token);
}
struct drgn_error *drgn_lexer_push(struct drgn_lexer *lexer,
const struct drgn_token *token)
{
if (lexer->stack_len >= lexer->stack_capacity) {
size_t new_capacity;
new_capacity = (lexer->stack_capacity ?
2 * lexer->stack_capacity : 2);
if (!resize_array(&lexer->stack, new_capacity))
return &drgn_enomem;
lexer->stack_capacity = new_capacity;
}
lexer->stack[lexer->stack_len++] = *token;
return NULL;
}
struct drgn_error *drgn_lexer_peek(struct drgn_lexer *lexer,
struct drgn_token *token)
{
struct drgn_error *err;
err = drgn_lexer_pop(lexer, token);
if (!err)
err = drgn_lexer_push(lexer, token);
return err;
}