Skip to content

Commit

Permalink
Add vc/comptime
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jan 12, 2025
1 parent 248aad9 commit b4a3f8f
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 103 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Master

- TBD
- Don't make things const by default as we don't get `const` info from the jextracts anyway
- Add `vc/comptime`
- Support nested components in VybeC

## v0.7.469

Expand Down
2 changes: 1 addition & 1 deletion resources/shaders/edge_2d.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ out vec4 finalColor;
// #define BLUENOISE_TEXTURE_RESOLUTION u_noiseResolution

#include "lygia/sample/clamp2edge.glsl"
#define EDGE_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV).r
#define EDGE_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV).g
#include "lygia/filter/edge.glsl"

void main (void) {
Expand Down
1 change: 1 addition & 0 deletions src/data_readers.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{vp/& vybe.panama/&-reader}
227 changes: 161 additions & 66 deletions src/vybe/c.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,22 @@

(defn ->name
[component-or-var]
(let [var-str (cond
(vp/component? component-or-var)
(vp/comp-name component-or-var)

(:no-ns (meta component-or-var))
(name (symbol component-or-var))

:else
(str (symbol component-or-var)))]
(-> var-str
(str/replace #"\." "_")
(str/replace #"/" "___")
(str/replace #"-" "_"))))
(try
(let [var-str (cond
(vp/component? component-or-var)
(name component-or-var)

(:no-ns (meta component-or-var))
(name (symbol component-or-var))

:else
(str (symbol component-or-var)))]
(-> var-str
(str/replace #"\." "_DOT_")
(str/replace #"/" "_SLASH_")
(str/replace #"-" "_DASH_")))
(catch Exception e
(throw (ex-info "Error in ->name" {:component-or-var component-or-var} e)))))

(defn- schema-adapter
[v]
Expand All @@ -105,7 +108,7 @@
:pointer

(vp/component? v)
(symbol (vp/comp-name v))
(symbol (->name v))

:else
v))
Expand All @@ -127,6 +130,12 @@
(->> type*
(walk/prewalk (fn prewalk [v]
(cond
(and (vector? v)
(contains? #{:vec}
(first v)))
(str (prewalk (last v))
"[]")

(and (vector? v)
(contains? #{:pointer :*}
(first v)))
Expand All @@ -148,7 +157,8 @@
(if-let [resolved (and (symbol? v)
(resolve v))]
(->name @resolved)
(name v)))))))))
(->name v)))))))))
#_(-adapt-type [:vec Rate])
#_(-adapt-type [:* {:const true} Rate])
#_(-adapt-type [:* Rate])
#_(-adapt-type :pointer)
Expand Down Expand Up @@ -204,40 +214,81 @@
[:bb [:* [:* Unit]]]])

(defn- comp->c
[component]
(let [c-name (->name component)]
(format "typedef struct %s {\n%s\n} %s;"
c-name
(->> (vp/comp-fields component)
(sort-by (comp :idx last))
(mapv (fn [[k type]]
(if (vp/fn-descriptor? type)
(let [{:keys [ret args]} (vp/fn-descriptor->map type)]
(format " %s (*%s)(%s);"
(-adapt-type (:schema ret))
(name k)
(->> (mapv (fn [{:keys [symbol schema]}]
(str (-adapt-type schema) " " (->name symbol)))
args)
(str/join ", "))))
(str " " (cond
(and (vector? type)
(= (first type) :pointer))
(-adapt-type type)

(= type :pointer)
"void*"

(= type :string)
"char*"

:else
(name type))
" " (name k) ";"))))
(str/join "\n"))
c-name)))
#_ (comp->c VybeAllocator)
#_ (comp->c Unit)
([component]
(comp->c component {}))
([component {:keys [level embedded]
:or {level 0
embedded false}
:as opts}]
(let [c-name (->name component)
opts (assoc opts :level level)
nesting (str/join "" (repeat (* (inc level) 2) \space))]
(format "%sstruct %s {\n%s\n%s}%s"
(if embedded
""
"typedef ")
c-name
(->> (vp/comp-fields component)
(sort-by (comp :idx last))
(mapv (fn [[k type]]
(if (vp/fn-descriptor? type)
(let [{:keys [ret args]} (vp/fn-descriptor->map type)]
(format "%s (*%s)(%s);"
(-adapt-type (:schema ret))
(name k)
(->> (mapv (fn [{:keys [symbol schema]}]
(str (-adapt-type schema) " " (->name symbol)))
args)
(str/join ", "))))
(cond
(and (vector? type)
(= (first type) :vec))
(let [[_ {:keys [size]} vec-type] type]
(format "%s %s[%s];"
(if (vp/component? vec-type)
(comp->c vec-type (merge (update opts :level inc)
{:embedded true}))
(-adapt-type vec-type))
(name k)
size))

:else
(str (cond
(and (vector? type)
(= (first type) :pointer))
(-adapt-type type)

(= type :pointer)
"void*"

(= type :string)
"char*"

:else
(try
(if (vp/component? type)
(comp->c type (merge (update opts :level inc)
{:embedded true}))
(name type))
#_(name type)
(catch Exception e
(throw (ex-info "Error on `comp->c` when getting name"
{:k k
:type type
:component component}
e)))))
" " (name k) ";")))))
(mapv #(str nesting %))
(str/join "\n"))
(if embedded
(str/join "" (repeat (* level 2) \space))
"")
(if embedded
""
(str " " c-name ";"))))))
#_ (println (comp->c vybe.flecs/system_desc_t))
#_ (println (comp->c VybeAllocator))
#_ (println (comp->c Unit))

(def -clz-h
"From clz.h in SC."
Expand All @@ -249,6 +300,9 @@
# include <stdbool.h>
#endif // __cplusplus
typedef bool boolean;
typedef char byte;
typedef int SCErr;
typedef int64_t int64;
Expand Down Expand Up @@ -610,11 +664,13 @@ static inline int32 NEXTPOWEROFTWO(int32 x) { return (int32)1L << LOG2CEIL(x); }
([form]
(analyze form (ana/empty-env) {}))
([form env]
(analyze form env {}))
(analyze form (or env (ana/empty-env)) {}))
([form env opts]
(try
(ana/analyze form env (merge {:bindings {#'ana-/macroexpand-1 -macroexpand-1}}
opts))
(ana/analyze form
(or env (ana/empty-env))
(merge {:bindings {#'ana-/macroexpand-1 -macroexpand-1}}
opts))
(catch Exception ex
(let [data {:form form
:env env
Expand Down Expand Up @@ -670,9 +726,10 @@ static inline int32 NEXTPOWEROFTWO(int32 x) { return (int32)1L << LOG2CEIL(x); }
(->name (resolve name))
(->> params
(mapv (fn [{:keys [tag form]}]
(str (if (:mut (meta form))
""
"const ")
(str #_(if (:mut (meta form))
""
"const ")
""
(case (.getName ^Class tag)
"[F"
"float*"
Expand Down Expand Up @@ -754,7 +811,13 @@ signal(SIGSEGV, sighandler);
(format "{%s}"
(->> (:val v)
(mapv (fn [[k v]]
(str "." (name k) " = " v)))
(str "." (name k) " = "
(emit (analyze v)))))
(str/join ", ")))

(= (:type v) :vector)
(format "{%s}"
(->> (:val v)
(str/join ", ")))

(string? (:val v))
Expand All @@ -773,6 +836,11 @@ signal(SIGSEGV, sighandler);
nil "NULL"
(:val v)))

:vector
(format "{%s}"
(->> (mapv emit (:items v))
(str/join ", ")))

:static-call
(let [{:keys [method args] klass :class} v]
(case (->sym klass)
Expand Down Expand Up @@ -939,7 +1007,7 @@ signal(SIGSEGV, sighandler);
(->name (:var v))

(vp/component? var-value)
(->name (vp/comp-name var-value))
(->name var-value)

(get-method c-replace my-var)
(c-replace v)
Expand Down Expand Up @@ -978,9 +1046,10 @@ signal(SIGSEGV, sighandler);
""
;; Variables are defined as
;; `const` by default.
(if (:mut (meta form))
"__auto_type "
"const __auto_type "))
#_(if (:mut (meta form))
"__auto_type "
"const __auto_type ")
"__auto_type ")
form
(emit init))]
(-> acc
Expand Down Expand Up @@ -1060,8 +1129,7 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
(mapv (fn [c]
(let [n (-adapt-type c)]
(format " struct %s: \"%s\"" n (vp/comp-name c)))))
(str/join ", \\\n"))
)
(str/join ", \\\n")))
"")))
#_ (-typename-schemas [VybeHooks])

Expand Down Expand Up @@ -1260,7 +1328,7 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i

{:keys [c-code ::c-data form-hash final-form init-struct-val]}
(-> code-form
(transpile (assoc opts ::version 30)))
(transpile (assoc opts ::version 33)))

obj-name (str "vybe_" sym-name "_"
(when (or (:no-cache sym-meta)
Expand Down Expand Up @@ -1498,7 +1566,13 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
(defonce -*vybec-fn-watchers (atom {}))

(defmacro defn*
"Transpiles Clojure code into a C function."
"Transpiles Clojure code into a C function.
E.g.
(vc/defn* simple-10 :- :int
[v :- Translation]
(simple v))"
{:clj-kondo/lint-as 'schema.core/defn}
[n _ ret-schema args & fn-tail]
`(let [args-desc-1# (quote ~(->> args
Expand Down Expand Up @@ -1674,6 +1748,16 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
:data (vybe.type/Vector2 [10 5])
:metadata "MY META"}))
(defn comptime
"In the JVM, it returns `v`. In VybeC, it runs the
code in compile time (in the JVM) and returns the value."
[v]
v)
(defmethod c-macroexpand #'comptime
[{:keys [form]}]
(eval form))
(declare ^:no-ns typeof
^:no-ns typename
#_^:no-ns comptime)
Expand All @@ -1687,7 +1771,7 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
:metadata ~(let [{:keys [line column]} (meta form)]
(str *ns* ":" line ":" column))
:form ~(str form)
:data (vp/address arg#)}))
:data (vp/& arg#)}))
arg#))
;; ================= c-invoke methods ===================
Expand Down Expand Up @@ -1728,7 +1812,7 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
(emit (analyze arg-form (:env arg)))))
;; Map.
(some-> arg emit))]
(str "(" (->name v) ")"
(str "(" (->name @v) ")"
(or arg-str "{}"))))

;; -- Clojure core.
Expand Down Expand Up @@ -1777,6 +1861,17 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
[{:keys [args]}]
(format "&%s" (emit (first args))))

(defmethod c-invoke #'vp/&
[{:keys [args]}]
(format "&%s" (emit (first args))))

(defmethod c-invoke #'vp/as
[{:keys [args]}]
(def args args)
(format "(%s)%s"
(-adapt-type (:val (second args)))
(emit (first args))))

(defmethod c-invoke #'vp/sizeof
[{:keys [args]}]
(if (= (count args) 2)
Expand Down
Loading

0 comments on commit b4a3f8f

Please sign in to comment.