Commits

Jason S  committed 9c0a0fa

new testcase for plain stuff in llvm

  • Participants
  • Parent commits 379e091

Comments (0)

Files changed (6)

File plain/src/fibfuncs.cpp

+unsigned short ufib(unsigned short n)
+{
+  return n <= 1 ? n : ufib(n-1) + ufib(n-2);
+}
+
+short sfib(short n)
+{
+  return n <= 1 ? n : sfib(n-1) + sfib(n-2);
+}
+
+int ifib(int n)
+{
+  return n <= 1 ? n : ifib(n-1) + ifib(n-2);
+}
+
+int iterfib(int n)
+{
+  int p0 = 0;
+  int p1 = 1;
+  while (n > 1)
+  {
+    int p2 = p0+p1;
+    p0 = p1;
+    p1 = p2;
+    --n;
+  }
+  return p1;
+}

File plain/src/main.cpp

+#include <stdio.h>
+#include "shared.h"
+#include "fibfuncs.h"
+
+extern int foo(int n, int x);
+extern "C" {
+  extern float bar(float x, int n);
+  extern void baz(const char *label, int k);
+  extern void fbaz(const char *label, float f);
+  extern void dbaz(const char *label, double f);
+  extern intpair_t exteuc(intpair_t ab, int *pgcd);
+}
+
+int main()
+{
+	int x = 3;
+	int y = foo(x,7);
+	int z = sfib(x);
+	int z2 = ufib(x);
+	int z3 = ifib(x);
+	float w = 0.577;
+	float v = bar(w, 4);
+	baz("x", x);
+	baz("y", y);
+	baz("z", z);
+	baz("z2", z2);
+	fbaz("w", w);
+	dbaz("v", v);
+	
+	intpair_t ab;
+	ab.a = 14;
+	ab.b = 37;
+	int gcd = 0;
+	
+	intpair_t xy = exteuc(ab, &gcd);
+	printf("%d*%d + %d*%d = %d\n", ab.a, xy.a, ab.b, xy.b, gcd);
+}

File plain/src/shared.h

+#ifndef __SHARED_H
+#define __SHARED_H
+
+struct intpair_t
+{
+  int a;
+  int b;
+};
+
+#endif

File plain/src/sub1.cpp

+#include <stdio.h>
+#include "shared.h"
+
+int foo(int n, int x)
+{
+  int y = n*x;
+  if (n > 1)
+    y += x*foo(n-1, x);
+  return y;
+}
+
+extern "C" {
+  float bar(float x, int n)
+  {
+    float y = n*x;
+    if (n > 1)
+      y += x*bar(n-1, x);
+    return y;
+  }
+  
+  void baz(const char *label, int k)
+  {
+    printf("%s = %d\n", label, k);
+  }
+
+  void fbaz(const char *label, float f)
+  {
+    printf("%s = %f\n", label, f);
+  }
+  
+  void dbaz(const char *label, double f)
+  {
+    printf("%s = %f\n", label, f);
+  }
+  
+  intpair_t exteuc(intpair_t ab, int *pgcd)
+  {
+    intpair_t xyprev, xy;
+    xyprev.a = 1;
+    xyprev.b = 0;
+    xy.a = 0;
+    xy.b = 1;
+    while (ab.b != 0)
+    {
+      int q = ab.a / ab.b;
+      int nextx = xyprev.a - q*xy.a;
+      int nexty = xyprev.b - q*xy.b;
+      xyprev = xy;
+      xy.a = nextx;
+      xy.b = nexty;
+      
+      int nexta = ab.b;
+      ab.b = ab.a - ab.b*q;
+      ab.a = nexta;
+    }
+    *pgcd = ab.a;
+    return xyprev;    
+  }
+}

File plain/src/wscript

+def configure(ctx):
+ pass
+
+
+def link_bitcode(ctx, modules, output_base):
+ ctx(rule='${LLVM_LD} ${SRC} -b ${TGT}', source=[name+'.bc' for name in modules], target=[output_base+'.bc'])
+ 
+def use_bitcode(ctx, output_base):
+ ctx(rule='${LLVM_DIS} ${SRC} -o=${TGT}', source=output_base+'.bc', target=output_base+'.ll')
+ ctx(rule='${LLC} -O=3 ${SRC} -o ${TGT}', source=output_base+'.bc', target=output_base+'.s')
+ ctx(rule='${CLANG_PP} ${SRC} -o ${TGT}', source=output_base+'.bc', target=output_base)
+
+def build(ctx):
+ print ctx.env.CLANG
+ modules = ('main', 'sub1', 'fibfuncs')
+ for name in modules:
+       ctx(rule='${CLANG} ${CXXFLAGS} -c ${SRC} -o ${TGT}', source=name+'.cpp', target=name+'.bc')
+ link_bitcode(ctx, modules, 'plain')
+ use_bitcode(ctx, 'plain')

File plain/wscript

+top = '.'
+out = './build'
+
+def configure(ctx):
+ ctx.env.CXXFLAGS = ['-O2', '-emit-llvm']
+ paths = ['/usr/bin', '/opt/local/bin', '/opt/local/libexec/llvm-3.1/bin']
+ ctx.find_program('clang', var='CLANG', path_list=paths)
+ ctx.find_program('clang++', var='CLANG_PP', path_list=paths)
+ ctx.find_program('llvm-as', var='LLVM_AS', path_list=paths)
+ ctx.find_program('llvm-dis', var='LLVM_DIS', path_list=paths)
+ ctx.find_program('llvm-ld', var='LLVM_LD', path_list=paths)
+ ctx.find_program('llc', var='LLC', path_list=paths)
+ ctx.find_program('gcc', var='GCC', path_list=paths)
+ ctx.recurse('src')
+
+def build(ctx):
+  ctx.recurse('src')
+