Advertisement
Guest User

Untitled

a guest
Feb 25th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns main)
  2.  
  3. (require '[clojure.string :as string])
  4.  
  5. (defprotocol llvm_Code
  6.   "A protocol for all types that map to llvm"
  7.   (genCode [this] "llvm code version of the type"))
  8.  
  9. (defrecord llvm_Integer [precision]
  10.   llvm_Code
  11.   (genCode [this]
  12.     (str "i" precision)))
  13.  
  14. (defrecord llvm_Struct [types]
  15.   llvm_Code
  16.   (genCode [this]
  17.            (str "{" (string/join "," (map genCode types)) "}")))
  18.  
  19. (defrecord llvm_Vector [elements type]
  20.   llvm_Code
  21.   (genCode [this]
  22.     (str "<" elements 'x (genCode type) ">")))
  23.  
  24. (defrecord llvm_Double []
  25.   llvm_Code
  26.   (genCode [this]
  27.            (str "double")))
  28.  
  29. (defrecord llvm_Void []
  30.   llvm_Code
  31.   (genCode [this]
  32.            (str "void")))
  33.  
  34. (defn mangle [x]
  35.    (-> x
  36.      (string/replace "_" "__")
  37.      (string/replace ":" "_colon_")))
  38.  
  39. (defn argPairGen [x]
  40.   (str (genCode (x 0)) " %" (mangle (x 1))))
  41.  
  42. (defn argListGenCode [x]
  43.   (string/join ", " (map argPairGen x)))
  44.  
  45. (defrecord llvm_Define [ret funcName args body]
  46.   llvm_Code
  47.   (genCode [this]
  48.            (str "define " (genCode ret) " @" funcName "(" (argListGenCode args)  "){  }")))
  49.  
  50. (defrecord llvm_Pointer [toType]
  51.   llvm_Code
  52.   (genCode [this]
  53.            (str (genCode toType) "*")))
  54.  
  55. (defrecord llvm_Block [blocks]
  56.   llvm_Code
  57.   (genCode [this]
  58.            (string/join "\n" (map genCode blocks))))
  59.  
  60. (defrecord llvm_RetValue [retType value]
  61.   llvm_Code
  62.   (genCode [this]
  63.            (str (genCode retType) " " (genCode value))))
  64.  
  65. (defrecord llvm_RetVoid [a]
  66.   llvm_Code
  67.   (genCode [this] "ret void"))
  68.  
  69. (defrecord llvm_Let [bindName statement]
  70.   llvm_Code
  71.   (genCode [this]
  72.            (str '% (mangle bindName) " = " (genCode statement))))
  73.  
  74. ;;; USABLE STUFF BELOW
  75.  
  76. (defn lint
  77.   "An specific precisiti on integer."
  78.   [n]
  79.     (assert (integer? n))
  80.     (llvm_Integer. n))
  81.  
  82. (defn lstruct [& types]
  83.   (assert true) ; TODO if types is a map
  84.   ; (apply print (map #(extends? llvm_Type %) types))
  85.   (llvm_Struct. types))
  86.  
  87. (defn lret
  88.   ([] (llvm_RetVoid. nil))
  89.   ([t v] (llvm_RetValue. t v)))
  90.  
  91. (defn lp
  92.   "llvm pointer"
  93.   [t] (llvm_Pointer. t))
  94.  
  95. (defn llet
  96.   ([bindName value] (llvm_Let. bindName value)))
  97.  
  98. (defn ldef [ret fname argList body]
  99.   (llvm_Define. ret fname argList body))
  100.  
  101. (defn lvoid llvm_Void.)
  102.  
  103.  
  104. ;; User-defined code
  105.  
  106. (def lint32 (lint 32))
  107.  
  108. (def lchar (lint8))
  109.  
  110. (print (genCode (ldef lvoid :main [[lin32 :argc]
  111.                                    [(lp (lp lchar)) :argv]]
  112.                       :body)))
  113.  
  114.  
  115. ;; wanna be code
  116.  
  117. ;(ldecl printf ((lp li8), lvarargs))
  118. ;
  119. ;(ldefn lint32 main [[lint32 argc]
  120. ;                    [(lp (lp lchar)) argv]]
  121. ;       (llet v (printf "Hello World!"))
  122. ;       (lret v))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement