Browse Source

Begin work on making packages import assembly sort files (.S)

gingerBill 4 years ago
parent
commit
b1bdd95f19
3 changed files with 75 additions and 10 deletions
  1. 1 3
      src/build_settings.cpp
  2. 48 1
      src/parser.cpp
  3. 26 6
      src/parser.hpp

+ 1 - 3
src/build_settings.cpp

@@ -291,10 +291,8 @@ TargetArchKind get_target_arch_from_string(String str) {
 
 
 bool is_excluded_target_filename(String name) {
-	String const ext = str_lit(".odin");
 	String original_name = name;
-	GB_ASSERT(string_ends_with(name, ext));
-	name = substring(name, 0, name.len-ext.len);
+	name = remove_extension_from_path(name);
 
 	String str1 = {};
 	String str2 = {};

+ 48 - 1
src/parser.cpp

@@ -4430,6 +4430,7 @@ void destroy_parser(Parser *p) {
 			destroy_ast_file(pkg->files[j]);
 		}
 		array_free(&pkg->files);
+		array_free(&pkg->foreign_files);
 	}
 #if 0
 	for_array(i, p->package_imports) {
@@ -4482,6 +4483,45 @@ void parser_add_file_to_process(Parser *p, AstPackage *pkg, FileInfo fi, TokenPo
 	thread_pool_add_task(&parser_thread_pool, parser_worker_proc, wd);
 }
 
+WORKER_TASK_PROC(foreign_file_worker_proc) {
+	ForeignFileWorkerData *wd = cast(ForeignFileWorkerData *)data;
+	Parser *p = wd->parser;
+	ImportedFile *imp = &wd->imported_file;
+	AstPackage *pkg = imp->pkg;
+
+	AstForeignFile foreign_file = {wd->foreign_kind};
+
+	String fullpath = string_trim_whitespace(imp->fi.fullpath); // Just in case
+
+	char *c_str = alloc_cstring(heap_allocator(), fullpath);
+	defer (gb_free(heap_allocator(), c_str));
+
+	gbFileContents fc = gb_file_read_contents(heap_allocator(), true, c_str);
+	foreign_file.source.text = (u8 *)fc.data;
+	foreign_file.source.len = fc.size;
+
+	switch (wd->foreign_kind) {
+	case AstForeignFile_S:
+		// TODO(bill): Actually do something with it
+		break;
+	}
+	gb_mutex_lock(&p->file_add_mutex);
+	array_add(&pkg->foreign_files, foreign_file);
+	gb_mutex_unlock(&p->file_add_mutex);
+	return 0;
+}
+
+
+void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg, AstForeignFileKind kind, FileInfo fi, TokenPos pos) {
+	// TODO(bill): Use a better allocator
+	ImportedFile f = {pkg, fi, pos, p->file_to_process_count++};
+	auto wd = gb_alloc_item(heap_allocator(), ForeignFileWorkerData);
+	wd->parser = p;
+	wd->imported_file = f;
+	wd->foreign_kind = kind;
+	thread_pool_add_task(&parser_thread_pool, foreign_file_worker_proc, wd);
+}
+
 
 // NOTE(bill): Returns true if it's added
 bool try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
@@ -4504,6 +4544,7 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path,
 	pkg->kind = kind;
 	pkg->fullpath = path;
 	array_init(&pkg->files, heap_allocator());
+	pkg->foreign_files.allocator = heap_allocator();
 
 	// NOTE(bill): Single file initial package
 	if (kind == Package_Init && string_ends_with(path, FILE_EXT)) {
@@ -4554,11 +4595,17 @@ bool try_add_import_path(Parser *p, String const &path, String const &rel_path,
 	for_array(list_index, list) {
 		FileInfo fi = list[list_index];
 		String name = fi.name;
-		if (string_ends_with(name, FILE_EXT)) {
+		String ext = path_extension(name);
+		if (ext == FILE_EXT) {
 			if (is_excluded_target_filename(name)) {
 				continue;
 			}
 			parser_add_file_to_process(p, pkg, fi, pos);
+		} else if (ext == ".S" || ext ==".s") {
+			if (is_excluded_target_filename(name)) {
+				continue;
+			}
+			parser_add_foreign_file_to_process(p, pkg, AstForeignFile_S, fi, pos);
 		}
 	}
 

+ 26 - 6
src/parser.hpp

@@ -121,14 +121,28 @@ struct AstFile {
 	struct LLVMOpaqueMetadata *llvm_metadata_scope;
 };
 
+enum AstForeignFileKind {
+	AstForeignFile_Invalid,
+
+	AstForeignFile_S, // Source,
+
+	AstForeignFile_COUNT
+};
+
+struct AstForeignFile {
+	AstForeignFileKind kind;
+	String source;
+};
+
 
 struct AstPackage {
-	PackageKind      kind;
-	isize            id;
-	String           name;
-	String           fullpath;
-	Array<AstFile *> files;
-	bool             is_single_file;
+	PackageKind           kind;
+	isize                 id;
+	String                name;
+	String                fullpath;
+	Array<AstFile *>      files;
+	Array<AstForeignFile> foreign_files;
+	bool                  is_single_file;
 
 	// NOTE(bill): Created/set in checker
 	Scope *   scope;
@@ -158,6 +172,12 @@ struct ParserWorkerData {
 	ImportedFile imported_file;
 };
 
+struct ForeignFileWorkerData {
+	Parser *parser;
+	ImportedFile imported_file;
+	AstForeignFileKind foreign_kind;
+};
+