Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dev/cppgm-scaffold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ bool consume_search_option(const vector<string>& args, size_t& i)
{
return true;
}
if (consume_joined_or_separate_option(args, i, "-isystem", "path"))
if (consume_joined_or_separate_option(args, i, "-isystem", "path") ||
consume_joined_or_separate_option(args, i, "-iquote", "path"))
{
return true;
}
Expand Down
12 changes: 11 additions & 1 deletion dev/cppgm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ struct SourceOutputInvocation
bool exceptions = true;
bool rtti = true;
vector<string> include_paths;
vector<string> quote_include_paths;
vector<string> system_include_paths;
string command_line;
bool wasm_atomics = true;
Expand Down Expand Up @@ -404,6 +405,11 @@ SourceOutputInvocation parse_source_output_invocation(const vector<string>& args
invocation.include_paths.push_back(i == option ? args[i].substr(2) : args[i]);
continue;
}
if (consume_joined_or_separate_option(args, i, "-iquote", "path"))
{
invocation.quote_include_paths.push_back(i == option ? args[i].substr(7) : args[i]);
continue;
}
if (consume_joined_or_separate_option(args, i, "-isystem", "path"))
{
invocation.system_include_paths.push_back(i == option ? args[i].substr(8) : args[i]);
Expand Down Expand Up @@ -536,7 +542,8 @@ bool consume_search_option(const vector<string>& args, size_t& i)
{
return true;
}
if (consume_joined_or_separate_option(args, i, "-isystem", "path"))
if (consume_joined_or_separate_option(args, i, "-isystem", "path") ||
consume_joined_or_separate_option(args, i, "-iquote", "path"))
{
return true;
}
Expand Down Expand Up @@ -756,6 +763,7 @@ PreprocessorOptions source_preprocessor_options(const SourceOutputInvocation& in
{
PreprocessorOptions options = source_preprocessor_options();
options.include_paths = invocation.include_paths;
options.quote_include_paths = invocation.quote_include_paths;
options.system_include_paths = invocation.system_include_paths;
options.command_line = invocation.command_line;
driver::configure_source_language(invocation.language, invocation.standard, options);
Expand All @@ -780,6 +788,8 @@ PreprocessorOptions source_preprocessor_options(const SourceOutputInvocation& in
}
else if (!invocation.sysroot.empty() || !invocation.resource_directory.empty())
throw InvocationError("target header directories currently require a Wasm target");
if (!options.quote_include_paths.empty())
driver::prune_include_paths(options);
return options;
}

Expand Down
2 changes: 1 addition & 1 deletion dev/frontend_source_sets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FRONTEND_PHASE_BASENAMES_object := native/object/object_file native/object/elf_o
FRONTEND_PHASE_BASENAMES_link := native/link/routine_builder native/link/runtime_support native/link/runtime_library native/link/linker
# lowir2native links an executable needing link mode's runtime library.
FRONTEND_OBJ_BASENAMES_lowir2native += $(FRONTEND_PHASE_BASENAMES_object) $(FRONTEND_PHASE_BASENAMES_link)
FRONTEND_OBJ_BASENAMES_cppgm += $(FRONTEND_PHASE_BASENAMES_native) $(FRONTEND_PHASE_BASENAMES_object) $(FRONTEND_PHASE_BASENAMES_link) driver/host_environment driver/wasm_environment driver/toolchain
FRONTEND_OBJ_BASENAMES_cppgm += $(FRONTEND_PHASE_BASENAMES_native) $(FRONTEND_PHASE_BASENAMES_object) $(FRONTEND_PHASE_BASENAMES_link) driver/host_environment driver/wasm_environment driver/native_assembly driver/toolchain
# PA33: the optimizer, and the LowIR reader for LowIR object inputs.
FRONTEND_OBJ_BASENAMES_cppgm += lowir/text_lexer lowir/text_parser lowir/text_parser_debug lowir/text_parser_debug_scopes lowir/text_parser_body lowir/text_parser_assembly lowir/text_reader $(FRONTEND_PHASE_BASENAMES_opt)
FRONTEND_OBJ_BASENAMES_cppgm += asm/registers
Expand Down
25 changes: 25 additions & 0 deletions dev/src/asm/gnu_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,31 @@ uint32_t reference(const std::string& source, std::size_t& pos, const std::vecto
void pieces(PacketBuilder& packet, const std::string& source, const std::vector<GnuOperand>& operands, bool basic)
{
std::string literal;
bool alternatives = false;
bool selected = true;
for (std::size_t pos = 0; pos < source.size();)
{
const char c = source[pos++];
// Source GNU packets use AT&T, the first dialect alternative.
if (!basic && c == '{')
{
if (alternatives)
fail("nested assembly dialect alternatives");
alternatives = true;
continue;
}
if (alternatives && (c == '|' || c == '}'))
{
alternatives = c != '}';
selected = !alternatives;
continue;
}
if (!selected)
{
if (c == '%' && pos < source.size())
++pos;
continue;
}
if (c != '%' || basic)
{
literal += c;
Expand Down Expand Up @@ -131,6 +153,7 @@ void pieces(PacketBuilder& packet, const std::string& source, const std::vector<
piece.bits = 512;
break;
case 'P':
case 'X': // Raw spelling without a PIC suffix.
case 'c':
piece.format = Format::Bare;
break;
Expand All @@ -157,6 +180,8 @@ void pieces(PacketBuilder& packet, const std::string& source, const std::vector<
}
packet.pieces.push_back(piece);
}
if (alternatives)
fail("unterminated assembly dialect alternatives");
text(packet, literal);
}

Expand Down
59 changes: 59 additions & 0 deletions dev/src/driver/host_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,65 @@

namespace driver {

// GCC's include chain (cpplib's remove_duplicates): a directory named
// twice is searched at its first position only, and an -I directory that
// is also a system directory (-isystem or a standard one) is searched as
// that system directory ("the -I option is ignored"), so <stdlib.h> under
// -I/usr/include still finds libstdc++'s wrapper first. Directories are
// compared by device and inode.
void prune_include_paths(PreprocessorOptions& options)
{
std::vector<std::pair<dev_t, ino_t>> seen;
const auto identity = [](const std::string& path, std::pair<dev_t, ino_t>& id) {
struct stat status;
if (stat(path.c_str(), &status) != 0 || !S_ISDIR(status.st_mode))
return false;
id = std::make_pair(status.st_dev, status.st_ino);
return true;
};
const auto first = [&](const std::string& path) {
std::pair<dev_t, ino_t> id;
if (!identity(path, id))
return true;
for (const std::pair<dev_t, ino_t>& other : seen)
{
if (other == id)
return false;
}
seen.push_back(id);
return true;
};
std::vector<std::string> system;
for (const std::string& path : options.system_include_paths)
{
if (first(path))
system.push_back(path);
}
std::vector<std::string> user;
for (const std::string& path : options.include_paths)
{
if (first(path))
user.push_back(path);
}
if (!options.quote_include_paths.empty())
{
seen.clear();
std::vector<std::string> quote;
for (const std::string& path : options.quote_include_paths)
if (first(path))
quote.push_back(path);
// CPP joins the two chains, coalescing only a repeated boundary.
const std::vector<std::string>& angle = user.empty() ? system : user;
std::pair<dev_t, ino_t> before, after;
if (!quote.empty() && !angle.empty() && identity(quote.back(), before) && identity(angle.front(), after) &&
before == after)
quote.pop_back();
options.quote_include_paths.swap(quote);
}
options.system_include_paths.swap(system);
options.include_paths.swap(user);
}

void configure_target_environment(PreprocessorOptions& options, native::Target target)
{
options.target = target;
Expand Down
3 changes: 3 additions & 0 deletions dev/src/driver/host_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ bool add_host_environment(
// Foreign SDK paths must be explicit; baked host paths are not a foreign SDK.
void configure_target_environment(PreprocessorOptions& options, native::Target target);

// Canonicalize the GNU include search chains by filesystem identity.
void prune_include_paths(PreprocessorOptions& options);

// Builtin templates have language semantics independent of va_list or layout.
const char* builtin_template_declarations();

Expand Down
89 changes: 89 additions & 0 deletions dev/src/driver/native_assembly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "driver/native_assembly.h"

#include <iostream>

#include "asm/module_expand.h"
#include "native/asm/module.h"
#include "native/backend_error.h"
#include "preprocess/preprocessor.h"

namespace driver {
namespace {

std::string assembly_source(Preprocessor& preprocessor)
{
std::string source;
bool comment = false;
for (;;)
{
const PPToken& token = preprocessor.next();
if (token.kind == PPTokenKind::EndOfFile)
return source;
if ((token.flags & kStartOfLine) != 0)
{
source.push_back('\n');
comment = false;
}
if (token.punctuator == kHashPunctuator)
comment = true;
if (comment || token.kind == PPTokenKind::WhitespaceSequence || token.kind == PPTokenKind::NewLine)
continue;
if ((token.flags & kLeadingSpace) != 0)
source.push_back(' ');
source.append(token.spelling, token.length);
}
}

} // namespace

bool compile_native_assembly(
const std::string& path, const PreprocessorOptions& base, native::Target target, native::object::ObjectFile& object)
{
PreprocessorOptions options = base;
options.assembler = true;
options.language = SourceLanguage::C;
options.cplusplus.clear();
options.builtin_declarations.clear();
try
{
Preprocessor preprocessor(options, path);
try
{
assembly::ModuleExpander expander;
const auto lines = expander.expand(assembly::module_lines(assembly_source(preprocessor), {}));
object.path = path;
native::ModuleAssembler assembler(object, target);
assembler.begin(asm_model::Dialect::Att);
for (const auto& line : lines)
assembler.declare(line);
for (const auto& line : lines)
assembler.append(line);
assembler.finish();
return true;
}
catch (const PreprocessError& failure)
{
std::cerr << "ERROR: " << preprocessor.describe(failure) << '\n';
return false;
}
}
catch (const assembly::AssemblyError& failure)
{
std::cerr << "ERROR: " << path << ':' << failure.line << ": " << failure.message << '\n';
}
catch (const native::BackendError& failure)
{
std::cerr << "ERROR: " << path << ": " << failure.what() << '\n';
}
catch (const SourceError& failure)
{
std::cerr << "ERROR: " << path << ": " << failure.what() << '\n';
}
catch (const PreprocessError& failure)
{
std::cerr << "ERROR: " << path << ": " << failure.what() << '\n';
}
return false;
}

} // namespace driver
20 changes: 20 additions & 0 deletions dev/src/driver/native_assembly.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <string>

#include "native/target.h"

struct PreprocessorOptions;

namespace native {
namespace object {
struct ObjectFile;
}
}

namespace driver {

bool compile_native_assembly(const std::string& path, const PreprocessorOptions& base, native::Target target,
native::object::ObjectFile& object);

} // namespace driver
Loading
Loading