Skip to content

Commit

Permalink
Merge pull request #246 from chaoticgd/bitfields
Browse files Browse the repository at this point in the history
Improve support for bitfields and 128-bit values
  • Loading branch information
chaoticgd authored Oct 19, 2024
2 parents 0870d62 + 338a93e commit bb4d923
Show file tree
Hide file tree
Showing 16 changed files with 984 additions and 100 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ add_library(ccc STATIC
src/ccc/elf_symtab.h
src/ccc/importer_flags.cpp
src/ccc/importer_flags.h
src/ccc/int128.cpp
src/ccc/int128.h
src/ccc/mdebug_analysis.cpp
src/ccc/mdebug_analysis.h
src/ccc/mdebug_importer.cpp
Expand Down Expand Up @@ -93,6 +95,7 @@ add_library(ccc_platform STATIC
set(TEST_SOURCES
test/demangler_tests.cpp
test/collision_tests.cpp
test/ccc/int128_tests.cpp
test/ccc/mdebug_importer_tests.cpp
test/ccc/stabs_tests.cpp
test/ccc/symbol_database_tests.cpp
Expand Down
1 change: 1 addition & 0 deletions docs/JsonFormat.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and `ast_json.cpp` for the code that currently defines the format.

| Format Version | Release | Changes |
| - | - | - |
| 15 | v2.2 | The offset_bytes and bitfield_offset_bits properties of bitfield nodes now represent and are relative to the beginning of the storage unit respectively. |
| 14 | v2.1 | Added stack_frame_size property for function symbols. |
| 13 | v2.0 | Added size_bytes field to all nodes. Renamed data_type_handle property to just data_type (since it's not a handle). |
| 12 | | Added format and application properties to root object. Added hash property to function symbols. |
Expand Down
1 change: 1 addition & 0 deletions docs/ProjectStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- src/ccc/elf.cpp: Parses ELF files.
- src/ccc/elf_symtab.cpp: Parses the ELF symbol table.
- src/ccc/importer_flags.cpp: An enum and help information printing for importer configuration flags.
- src/ccc/int128.cpp: 128-bit integer types.
- src/ccc/mdebug_analysis.cpp: Accepts a stream of symbols and imports the data.
- src/ccc/mdebug_importer.cpp: Top-level file for parsing .mdebug symbol tables.
- src/ccc/mdebug_section.cpp: Parses the .mdebug binary format.
Expand Down
41 changes: 41 additions & 0 deletions src/ccc/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,47 @@ std::pair<const Node*, const DataType*> Node::physical_type(const SymbolDatabase
return const_cast<Node*>(this)->physical_type(const_cast<SymbolDatabase&>(database), max_depth);
}

BuiltInClass BitField::storage_unit_type(const SymbolDatabase& database) const
{
BuiltInClass result = BuiltInClass::VOID_TYPE;

if(underlying_type.get()) {
auto [type, symbol] = underlying_type->physical_type(database);
switch(type->descriptor) {
case BUILTIN:
result = type->as<BuiltIn>().bclass;
break;
case ENUM:
result = BuiltInClass::SIGNED_32;
break;
default:
break;
}
}

return result;
}

u128 BitField::unpack_unsigned(u128 storage_unit) const
{
return (storage_unit >> bitfield_offset_bits) & ((u128(1) << size_bits) - 1);
}

s128 BitField::unpack_signed(u128 storage_unit) const
{
return s128(storage_unit << (128 - (bitfield_offset_bits + size_bits))) >> (128 - size_bits);
}

u128 BitField::pack_unsigned(u128 bitfield) const
{
return (bitfield & ((u128(1) << size_bits) - 1)) << bitfield_offset_bits;
}

u128 BitField::pack_signed(s128 bitfield) const
{
return u128((bitfield & ((u128(1) << size_bits) - 1)) << (128 - size_bits)) >> (128 - bitfield_offset_bits - size_bits);
}

const char* member_function_modifier_to_string(MemberFunctionModifier modifier)
{
switch(modifier) {
Expand Down
23 changes: 15 additions & 8 deletions src/ccc/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "int128.h"
#include "symbol_database.h"

namespace ccc::ast {
Expand Down Expand Up @@ -97,14 +98,6 @@ struct Array : Node {
static const constexpr NodeDescriptor DESCRIPTOR = ARRAY;
};

struct BitField : Node {
s32 bitfield_offset_bits = -1; // Offset relative to the last byte (not the position of the underlying type!).
std::unique_ptr<Node> underlying_type;

BitField() : Node(DESCRIPTOR) {}
static const constexpr NodeDescriptor DESCRIPTOR = BITFIELD;
};

enum class BuiltInClass {
VOID_TYPE,
UNSIGNED_8, SIGNED_8, UNQUALIFIED_8, BOOL_8,
Expand All @@ -114,6 +107,20 @@ enum class BuiltInClass {
UNSIGNED_128, SIGNED_128, UNQUALIFIED_128, FLOAT_128
};

struct BitField : Node {
s32 bitfield_offset_bits = -1;
std::unique_ptr<Node> underlying_type;

BitField() : Node(DESCRIPTOR) {}
static const constexpr NodeDescriptor DESCRIPTOR = BITFIELD;

BuiltInClass storage_unit_type(const SymbolDatabase& database) const;
u128 unpack_unsigned(u128 storage_unit) const;
s128 unpack_signed(u128 storage_unit) const;
u128 pack_unsigned(u128 bitfield) const;
u128 pack_signed(s128 bitfield) const;
};

struct BuiltIn : Node {
BuiltInClass bclass = BuiltInClass::VOID_TYPE;

Expand Down
1 change: 1 addition & 0 deletions src/ccc/ccc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "elf.h"
#include "elf_symtab.h"
#include "importer_flags.h"
#include "int128.h"
#include "mdebug_analysis.h"
#include "mdebug_importer.h"
#include "mdebug_section.h"
Expand Down
Loading

0 comments on commit bb4d923

Please sign in to comment.