From d0124f0fcf2e4fcd4895f08febba662c0a5fdac0 Mon Sep 17 00:00:00 2001
From: Andrea Shepard <andrea@persephoneslair.org>
Date: Thu, 24 Nov 2016 07:16:25 +0000
Subject: [PATCH] Implement h_epsilon_p() support in the LLVM backend

---
 src/parsers/epsilon.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/parsers/epsilon.c b/src/parsers/epsilon.c
index bb6e8beb..4c5abc40 100644
--- a/src/parsers/epsilon.c
+++ b/src/parsers/epsilon.c
@@ -1,3 +1,10 @@
+#ifdef HAMMER_LLVM_BACKEND
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpedantic"
+#include <llvm-c/Core.h>
+#pragma GCC diagnostic pop
+#include "../backends/llvm/llvm.h"
+#endif            
 #include "parser_internal.h"
 
 static HParseResult* parse_epsilon(void* env, HParseState* state) {
@@ -12,12 +19,45 @@ static bool epsilon_ctrvm(HRVMProg *prog, void* env) {
   return true;
 }
 
+#ifdef HAMMER_LLVM_BACKEND
+
+static bool epsilon_llvm(HLLVMParserCompileContext *ctxt, void* env) {
+  if (!ctxt) return false;
+
+  LLVMBasicBlockRef epsilon_bb = LLVMAppendBasicBlock(ctxt->func, "epsilon");
+
+  /* Basic block: epsilon */
+  LLVMBuildBr(ctxt->builder, epsilon_bb);
+  LLVMPositionBuilderAtEnd(ctxt->builder, epsilon_bb);
+
+  /*
+   * For epsilon we make a null-token parse result like with end, but we
+   * do it unconditionally.
+   */
+  LLVMValueRef make_result_args[] = {
+    ctxt->arena,
+    LLVMConstNull(ctxt->llvm_parsedtokenptr)
+  };
+  LLVMValueRef result_ptr = LLVMBuildCall(ctxt->builder,
+    LLVMGetNamedFunction(ctxt->mod, "make_result"),
+    make_result_args, 2, "result_ptr");
+  /* Return it */
+  LLVMBuildRet(ctxt->builder, result_ptr);
+
+  return true;
+}
+
+#endif
+
 static const HParserVtable epsilon_vt = {
   .parse = parse_epsilon,
   .isValidRegular = h_true,
   .isValidCF = h_true,
   .desugar = desugar_epsilon,
   .compile_to_rvm = epsilon_ctrvm,
+#ifdef HAMMER_LLVM_BACKEND
+  .llvm = epsilon_llvm,
+#endif
   .higher = false,
 };
 
-- 
GitLab