Skip to content

Commit dabb75b

Browse files
committed
Upgrade gcc, clang, boost
1 parent 5012cc9 commit dabb75b

18 files changed

+516
-743
lines changed

CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ set(BOOST_INCLUDE_LIBRARIES algorithm convert process program_options test)
1515
FetchContent_Declare(
1616
Boost
1717
GIT_REPOSITORY https://github.com/boostorg/boost.git
18-
GIT_TAG boost-1.80.0
18+
GIT_TAG boost-1.83.0
1919
GIT_SHALLOW ON
2020
)
2121

2222
set(BENCHMARK_ENABLE_TESTING FALSE)
2323
FetchContent_Declare(
2424
Google_Benchmark
2525
GIT_REPOSITORY https://github.com/google/benchmark.git
26-
GIT_TAG v1.7.1
26+
GIT_TAG v1.8.3
2727
GIT_SHALLOW ON
2828
)
2929

@@ -36,7 +36,7 @@ FetchContent_Declare(
3636

3737
FetchContent_Declare(
3838
Crafting_Interpreters
39-
URL https://github.com/munificent/craftinginterpreters/archive/d2800ebca6ab6d56f698ad0eef8202a91821eb04.zip
39+
URL https://github.com/munificent/craftinginterpreters/archive/01e6f5b8f3e5dfa65674c2f9cf4700d73ab41cf8.zip
4040
)
4141

4242
FetchContent_MakeAvailable(Boost Google_Benchmark Microsoft_GSL Crafting_Interpreters)

Dockerfile

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
ARG CC=gcc
1+
ARG CC=clang
22

33
FROM ubuntu:22.04 AS base
44

5-
RUN apt update && apt install -y cmake git ninja-build
5+
RUN apt update && apt install -y cmake git ninja-build software-properties-common
66

77
FROM base AS base-clang
88

9-
RUN apt update && apt install -y clang
9+
RUN apt update && apt install -y gpg lsb-release wget
10+
RUN wget https://apt.llvm.org/llvm.sh
11+
RUN chmod +x llvm.sh
12+
RUN ./llvm.sh 17
13+
ENV CC=clang-17
14+
ENV CXX=clang++-17
1015

1116
FROM base AS base-gcc
1217

13-
RUN apt update && apt install -y build-essential
18+
RUN add-apt-repository ppa:ubuntu-toolchain-r/test
19+
RUN apt update && apt install -y g++-13
20+
ENV CC=gcc-13
21+
ENV CXX=g++-13
1422

1523
FROM base-$CC AS deps
1624

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ When targeting `bench`, use Docker's `--progress=plain` option to see the result
7979

8080
--build-arg CC=<compiler>
8181

82-
The compiler can be one of: `gcc` or `clang`. Defaults to `gcc`.
82+
The compiler can be one of: `gcc` or `clang`. Defaults to `clang`.
8383

8484
## Use REPL in container shell
8585

src/chunk.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ namespace motts::lox
5252
reinterpret_cast<std::uint16_t&>(bytecode_.at(jump_begin_index_ - 2)) = jump_distance_big_endian;
5353
}
5454

55-
Chunk::Constants_vector::size_type Chunk::insert_constant(Dynamic_type_value value)
55+
std::size_t Chunk::insert_constant(Dynamic_type_value value)
5656
{
5757
const auto maybe_duplicate_iter = std::find(constants_.cbegin(), constants_.cend(), value);
5858
if (maybe_duplicate_iter != constants_.cend()) {
5959
const auto constant_index = maybe_duplicate_iter - constants_.cbegin();
60-
return gsl::narrow<Constants_vector::size_type>(constant_index);
60+
return gsl::narrow<std::size_t>(constant_index);
6161
}
6262

6363
const auto constant_index = constants_.size();
@@ -221,7 +221,7 @@ namespace motts::lox
221221
os << "Bytecode:\n";
222222
for (auto bytecode_iter = chunk.bytecode().cbegin(); bytecode_iter != chunk.bytecode().cend();) {
223223
const auto bytecode_index = bytecode_iter - chunk.bytecode().cbegin();
224-
const auto source_map_token = chunk.source_map_tokens().at(bytecode_index);
224+
const auto& source_map_token = chunk.source_map_tokens().at(bytecode_index);
225225
const auto opcode = static_cast<Opcode>(*bytecode_iter++);
226226

227227
// Some opcodes such as closure will print multiple lines.

src/chunk.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstddef>
4+
#include <cstdint>
45
#include <ostream>
56
#include <vector>
67

@@ -102,7 +103,7 @@ namespace motts::lox
102103
void to_next_opcode();
103104
};
104105

105-
Constants_vector::size_type insert_constant(Dynamic_type_value);
106+
std::size_t insert_constant(Dynamic_type_value);
106107

107108
public:
108109
// Read-only access.

src/compiler.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
#include "object.hpp"
1111
#include "scanner.hpp"
1212

13-
// Not exported (internal linkage).
14-
namespace
13+
namespace motts::lox
1514
{
16-
// Allow the internal linkage section to access names.
17-
using namespace motts::lox;
18-
19-
void ensure_token_is(const Token& token, Token_type expected)
15+
static void ensure_token_is(const Token& token, Token_type expected)
2016
{
2117
if (token.type != expected) {
2218
std::ostringstream os;
@@ -256,9 +252,7 @@ namespace
256252

257253
(*(function_chunks.end() - 2))
258254
->chunk.emit_closure(
259-
gc_heap.make<Function>(
260-
{interned_strings.get(std::string_view{""}), param_count, std::move(function_chunk.chunk)}
261-
),
255+
gc_heap.make<Function>({interned_strings.get(""), param_count, std::move(function_chunk.chunk)}),
262256
function_chunk.tracked_upvalues,
263257
fun_token
264258
);
@@ -937,10 +931,7 @@ namespace
937931
}
938932
}
939933
};
940-
}
941934

942-
namespace motts::lox
943-
{
944935
Chunk compile(GC_heap& gc_heap, Interned_strings& interned_strings, std::string_view source)
945936
{
946937
return Compiler{gc_heap, interned_strings, source}.compile();

src/interned_strings.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ namespace motts::lox
88
gc_heap_.on_destroy_ptr.push_back([this](const auto& control_block) {
99
const auto maybe_gc_str_iter = strings_by_ptr_.find(&control_block);
1010
if (maybe_gc_str_iter != strings_by_ptr_.cend()) {
11-
const auto gc_str = maybe_gc_str_iter->second;
12-
13-
strings_by_chars_.erase(*gc_str);
11+
strings_by_chars_.erase(*maybe_gc_str_iter->second);
1412
strings_by_ptr_.erase(maybe_gc_str_iter);
1513
}
1614
});
@@ -21,6 +19,11 @@ namespace motts::lox
2119
gc_heap_.on_destroy_ptr.pop_back();
2220
}
2321

22+
GC_ptr<const std::string> Interned_strings::get(const char* str)
23+
{
24+
return get(std::string_view{str});
25+
}
26+
2427
GC_ptr<const std::string> Interned_strings::get(std::string_view str)
2528
{
2629
const auto maybe_dup_iter = strings_by_chars_.find(str);

src/interned_strings.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace motts::lox
1818
Interned_strings(GC_heap&);
1919
~Interned_strings();
2020

21+
GC_ptr<const std::string> get(const char*);
2122
GC_ptr<const std::string> get(std::string_view);
2223
GC_ptr<const std::string> get(std::string&&);
2324
};

src/memory.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ namespace motts::lox
3737

3838
const auto not_marked_begin =
3939
std::partition(all_ptrs_.begin(), all_ptrs_.end(), [](const auto* control_block) { return control_block->marked; });
40-
4140
std::for_each(not_marked_begin, all_ptrs_.end(), [&](const auto* control_block) {
42-
for (const auto& fn : on_destroy_ptr) {
43-
fn(*control_block);
41+
for (const auto& on_destroy_fn : on_destroy_ptr) {
42+
on_destroy_fn(*control_block);
4443
}
4544

4645
n_allocated_bytes_ -= control_block->size();

src/memory.hpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ namespace motts::lox
88
{
99
class GC_heap;
1010

11-
// Every garbage collect-able object can be polymorphically "marked" and trace references.
11+
// Every garbage collect-able object can be polymorphically marked and trace references.
1212
struct GC_control_block_base
1313
{
1414
bool marked{false};
1515

1616
virtual ~GC_control_block_base() = default;
17-
virtual void trace_refs(GC_heap&) = 0;
17+
virtual void trace_refs(GC_heap&) const = 0;
1818
virtual std::size_t size() const = 0;
1919
};
2020

@@ -39,7 +39,7 @@ namespace motts::lox
3939
{
4040
}
4141

42-
void trace_refs(GC_heap& gc_heap) override
42+
void trace_refs(GC_heap& gc_heap) const override
4343
{
4444
trace_refs_trait(gc_heap, value);
4545
}
@@ -149,14 +149,11 @@ namespace motts::lox
149149
}
150150

151151
// The hash of GC_ptrs is the hash of the underlying control block pointers.
152-
namespace std
152+
template<typename User_value_type>
153+
struct std::hash<motts::lox::GC_ptr<User_value_type>>
153154
{
154-
template<typename User_value_type>
155-
struct hash<motts::lox::GC_ptr<User_value_type>>
155+
std::size_t operator()(motts::lox::GC_ptr<User_value_type> gc_ptr) const
156156
{
157-
std::size_t operator()(motts::lox::GC_ptr<User_value_type> gc_ptr) const
158-
{
159-
return std::hash<motts::lox::GC_control_block_base*>{}(gc_ptr.control_block);
160-
}
161-
};
162-
}
157+
return std::hash<motts::lox::GC_control_block_base*>{}(gc_ptr.control_block);
158+
}
159+
};

src/object.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ namespace motts::lox
3333
void trace_refs_trait(GC_heap& gc_heap, const Closure& closure)
3434
{
3535
mark(gc_heap, closure.function);
36-
for (const auto& upvalue : closure.upvalues) {
36+
for (const GC_ptr<Upvalue> upvalue : closure.upvalues) {
3737
mark(gc_heap, upvalue);
3838
}
39-
for (const auto& upvalue : closure.open_upvalues) {
39+
for (const GC_ptr<Upvalue> upvalue : closure.open_upvalues) {
4040
mark(gc_heap, upvalue);
4141
}
4242
}
@@ -45,7 +45,7 @@ namespace motts::lox
4545
void trace_refs_trait(GC_heap& gc_heap, const Function& function)
4646
{
4747
mark(gc_heap, function.name);
48-
for (const auto& value : function.chunk.constants()) {
48+
for (const Dynamic_type_value value : function.chunk.constants()) {
4949
std::visit(Mark_objects_visitor{gc_heap}, value);
5050
}
5151
for (const auto& source_map_token : function.chunk.source_map_tokens()) {
@@ -68,7 +68,7 @@ namespace motts::lox
6868
}
6969
}
7070

71-
Upvalue::Upvalue(std::vector<Dynamic_type_value>& stack_arg, std::vector<Dynamic_type_value>::size_type stack_index_arg)
71+
Upvalue::Upvalue(std::vector<Dynamic_type_value>& stack_arg, std::size_t stack_index_arg)
7272
: value_{Open{stack_arg, stack_index_arg}}
7373
{
7474
}
@@ -79,7 +79,7 @@ namespace motts::lox
7979
value_ = Closed{open.stack.at(open.stack_index)};
8080
}
8181

82-
std::vector<Dynamic_type_value>::size_type Upvalue::stack_index() const
82+
std::size_t Upvalue::stack_index() const
8383
{
8484
return std::get<Open>(value_).stack_index;
8585
}
@@ -90,7 +90,7 @@ namespace motts::lox
9090
return std::get<Closed>(value_).value;
9191
}
9292

93-
const auto& open = std::get<Open>(value_);
93+
const auto open = std::get<Open>(value_);
9494
return open.stack.at(open.stack_index);
9595
}
9696

src/object.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace motts::lox
3333
struct Closure
3434
{
3535
GC_ptr<Function> function;
36-
// An upvalue refers to a local variable in an enclosing function that the closure uses.
3736
std::vector<GC_ptr<Upvalue>> upvalues;
37+
3838
// Following Lua, we’ll use "open upvalue" to refer to an upvalue that points to a local variable still on the stack.
3939
std::vector<GC_ptr<Upvalue>> open_upvalues;
4040

@@ -77,7 +77,7 @@ namespace motts::lox
7777
// These two fields can be thought of as an iterator into the stack,
7878
// but iterators can be invalidated, so instead keep a stack reference and index.
7979
std::vector<Dynamic_type_value>& stack;
80-
const std::vector<Dynamic_type_value>::size_type stack_index;
80+
const std::size_t stack_index;
8181
};
8282

8383
struct Closed
@@ -88,10 +88,10 @@ namespace motts::lox
8888
std::variant<Open, Closed> value_;
8989

9090
public:
91-
Upvalue(std::vector<Dynamic_type_value>& stack, std::vector<Dynamic_type_value>::size_type stack_index);
91+
Upvalue(std::vector<Dynamic_type_value>& stack, std::size_t stack_index);
9292

9393
void close();
94-
std::vector<Dynamic_type_value>::size_type stack_index() const;
94+
std::size_t stack_index() const;
9595
const Dynamic_type_value& value() const;
9696
Dynamic_type_value& value();
9797
};

src/value.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
#include "object.hpp"
44

5-
// Not exported (internal linkage).
6-
namespace
5+
namespace motts::lox
76
{
8-
// Allow the internal linkage section to access names.
9-
using namespace motts::lox;
10-
117
struct Print_visitor
128
{
139
std::ostream& os;
@@ -67,10 +63,7 @@ namespace
6763
os << *str;
6864
}
6965
};
70-
}
7166

72-
namespace motts::lox
73-
{
7467
std::ostream& operator<<(std::ostream& os, Dynamic_type_value value)
7568
{
7669
std::visit(Print_visitor{os}, value);

0 commit comments

Comments
 (0)