Browse Source

Add `@(link_section=<string>)` for global variables

gingerBill 4 years ago
parent
commit
c440296ae8
5 changed files with 18 additions and 0 deletions
  1. 3 0
      src/check_decl.cpp
  2. 10 0
      src/checker.cpp
  3. 1 0
      src/checker.hpp
  4. 1 0
      src/entity.cpp
  5. 3 0
      src/llvm_backend.cpp

+ 3 - 0
src/check_decl.cpp

@@ -934,6 +934,9 @@ void check_global_variable_decl(CheckerContext *ctx, Entity *&e, Ast *type_expr,
 	if (ac.link_name.len > 0) {
 		e->Variable.link_name = ac.link_name;
 	}
+	if (ac.link_section.len > 0) {
+		e->Variable.link_section = ac.link_section;
+	}
 
 	if (e->Variable.is_foreign || e->Variable.is_export) {
 		String name = e->token.string;

+ 10 - 0
src/checker.cpp

@@ -2650,6 +2650,16 @@ DECL_ATTRIBUTE_PROC(var_decl_attribute) {
 			error(elem, "Expected a string value for '%.*s'", LIT(name));
 		}
 		return true;
+	} else if (name == "link_section") {
+		if (ev.kind == ExactValue_String) {
+			ac->link_section = ev.value_string;
+			if (!is_foreign_name_valid(ac->link_section)) {
+				error(elem, "Invalid link section: %.*s", LIT(ac->link_section));
+			}
+		} else {
+			error(elem, "Expected a string value for '%.*s'", LIT(name));
+		}
+		return true;
 	}
 	return false;
 }

+ 1 - 0
src/checker.hpp

@@ -109,6 +109,7 @@ struct AttributeContext {
 	bool    set_cold;
 	String  link_name;
 	String  link_prefix;
+	String  link_section;
 	isize   init_expr_list_count;
 	String  thread_local_model;
 	String  deprecated_message;

+ 1 - 0
src/entity.cpp

@@ -158,6 +158,7 @@ struct Entity {
 			Ast *      foreign_library_ident;
 			String     link_name;
 			String     link_prefix;
+			String     link_section;
 			bool       is_foreign;
 			bool       is_export;
 		} Variable;

+ 3 - 0
src/llvm_backend.cpp

@@ -14880,6 +14880,9 @@ void lb_generate_code(lbGenerator *gen) {
 				LLVMSetLinkage(g.value, LLVMInternalLinkage);
 			}
 		}
+		if (e->Variable.link_section.len > 0) {
+			LLVMSetSection(g.value, alloc_cstring(permanent_allocator(), e->Variable.link_section));
+		}
 
 		lbGlobalVariable var = {};
 		var.var = g;