on master help sign in
Add a prim__systemInfo primitive.

Add System.Info with os and backend functions. Add system info to JS and C backends.

commitcc9b104e90eb169f55fe288f958bb647ee6c966e authorniklas <niklas@mm.st> dateThu 29 May 2014, 16:11 (12 years ago) parent09dec50
12 files changed+67−2
idris.cabal+1 jsrts/Runtime-browser.js+11 jsrts/Runtime-node.js+11 libs/base/System/Info.idr+13 libs/base/base.ipkg+1 rts/Makefile+2−1 rts/idris_rts.c+13 rts/idris_rts.h+6 src/IRTS/CodegenC.hs+2 src/IRTS/CodegenJavaScript.hs+2 src/IRTS/Lang.hs+2 src/Idris/Primitives.hs+3−1
idris.cabal+1
@@ -90,6 +90,7 @@ Extra-source-files:
9090 libs/base/Language/Reflection/*.idr
9191 libs/base/Makefile
9292 libs/base/Network/*.idr
93+ libs/base/System/*.idr
9394 libs/base/System/Concurrency/*.idr
9495 libs/base/Syntax/*.idr
9596
jsrts/Runtime-browser.js+11
@@ -1,3 +1,14 @@
11var __IDRRT__print = function(s) {
22 console.log(s);
33};
4+
5+
6+var __IDRRT__systemInfo = function(index) {
7+ switch(index) {
8+ case 0:
9+ return "javascript";
10+ case 1:
11+ return navigator.platform;
12+ }
13+ return "";
14+}
jsrts/Runtime-node.js+11
@@ -4,3 +4,14 @@ var __IDRRT__print = (function() {
44 util.print(s);
55 };
66})();
7+
8+var __IDRRT__systemInfo = function(index) {
9+ var os = require('os')
10+ switch(index) {
11+ case 0:
12+ return "node";
13+ case 1:
14+ return os.platform();
15+ }
16+ return "";
17+}
libs/base/System/Info.idrnew file+13
@@ -0,0 +1,13 @@
1+module System.Info
2+
3+||| The Idris backend in use
4+backend : String
5+backend = prim__systemInfo 0
6+
7+||| The operating system in use.
8+os : String
9+os = prim__systemInfo 1
10+
11+||| The triple this program was targeted for
12+targetTriple : String
13+targetTriple = prim__systemInfo 2
libs/base/base.ipkg+1
@@ -6,6 +6,7 @@ modules = System,
66 Network.Cgi, Network.Socket,
77 Debug.Trace,
88
9+ System.Info,
910 System.Concurrency.Raw, System.Concurrency.Process,
1011
1112 Decidable.Decidable, Decidable.Order,
rts/Makefile+2−1
@@ -5,7 +5,8 @@ OBJS = idris_rts.o idris_heap.o idris_gc.o idris_gmp.o idris_bitstring.o \
55HDRS = idris_rts.h idris_heap.h idris_gc.h idris_gmp.h idris_bitstring.h \
66 idris_opts.h idris_stats.h mini-gmp.h idris_stdfgn.h idris_net.h
77CFLAGS:=-fPIC $(CFLAGS)
8−CFLAGS += $(GMP_INCLUDE_DIR) $(GMP)
8+CFLAGS += $(GMP_INCLUDE_DIR) $(GMP) -DIDRIS_TARGET_OS="\"$(OS)\""
9+CFLAGS += -DIDRIS_TARGET_TRIPLE="\"$(MACHINE)\""
910
1011ifeq ($(OS), windows)
1112 OBJS += windows/idris_stdfgn.o windows/idris_net.o
rts/idris_rts.c+13
@@ -506,6 +506,19 @@ VAL idris_strRev(VM* vm, VAL str) {
506506 return cl;
507507}
508508
509+VAL idris_systemInfo(VM* vm, VAL index) {
510+ int i = GETINT(index);
511+ switch(i) {
512+ case 0: // backend
513+ return MKSTR(vm, "c");
514+ case 1:
515+ return MKSTR(vm, IDRIS_TARGET_OS);
516+ case 2:
517+ return MKSTR(vm, IDRIS_TARGET_TRIPLE);
518+ }
519+ return MKSTR(vm, "");
520+}
521+
509522VAL MKBUFFERc(VM* vm, Buffer* buf) {
510523 Closure* cl = allocate(vm, sizeof(Closure) + sizeof *buf + buf->cap, 1);
511524 SETTY(cl, BUFFER);
rts/idris_rts.h+6
@@ -279,6 +279,12 @@ VAL idris_peekB64Native(VM* vm, VAL buf, VAL off);
279279VAL idris_peekB64LE(VM* vm, VAL buf, VAL off);
280280VAL idris_peekB64BE(VM* vm, VAL buf, VAL off);
281281
282+// system infox
283+// used indices:
284+// 0 returns backend
285+// 1 returns OS
286+VAL idris_systemInfo(VM* vm, VAL index);
287+
282288// Command line args
283289
284290extern int __idris_argc;
src/IRTS/CodegenC.hs+2
@@ -493,6 +493,8 @@ doOp v (LChInt ITNative) args = v ++ creg (last args)
493493doOp v (LChInt ITChar) args = doOp v (LChInt ITNative) args
494494doOp v (LIntCh ITNative) args = v ++ creg (last args)
495495doOp v (LIntCh ITChar) args = doOp v (LIntCh ITNative) args
496+
497+doOp v LSystemInfo [x] = v ++ "idris_systemInfo(vm, " ++ creg x ++ ")"
496498doOp v LNoOp args = v ++ creg (last args)
497499doOp _ op _ = "FAIL /* " ++ show op ++ " */"
498500
src/IRTS/CodegenJavaScript.hs+2
@@ -2508,6 +2508,8 @@ translateExpression (SOp op vars)
25082508 JSNum (JSInt 1),
25092509 JSBinOp "-" (JSProj (JSIdent v) "length") (JSNum (JSInt 1))
25102510 ]
2511+ | LSystemInfo <- op
2512+ , (arg:_) <- vars = jsCall "__IDRRT__systemInfo" [JSVar arg]
25112513 | LNullPtr <- op
25122514 , (_) <- vars = JSNull
25132515
src/IRTS/Lang.hs+2
@@ -55,6 +55,8 @@ data PrimFn = LPlus ArithTy | LMinus ArithTy | LTimes ArithTy
5555 -- Buffers
5656 | LAllocate
5757 | LAppendBuffer
58+ -- system info
59+ | LSystemInfo
5860 -- Note that for Bits8 only Native endianness is actually used
5961 -- and the user-exposed interface for Bits8 doesn't mention
6062 -- endianness
src/Idris/Primitives.hs+3−1
@@ -184,7 +184,9 @@ primitives =
184184 Prim (sUN "prim__allocate") (ty [AType (ATInt (ITFixed IT64))] BufferType) 1 (p_allocate)
185185 (1, LAllocate) total,
186186 Prim (sUN "prim__appendBuffer") (ty [BufferType, AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), AType (ATInt (ITFixed IT64)), BufferType] BufferType) 6 (p_appendBuffer)
187− (6, LAppendBuffer) partial
187+ (6, LAppendBuffer) partial,
188+ Prim (sUN "prim__systemInfo") (ty [AType (ATInt ITNative)] StrType) 1 (p_cantreduce)
189+ (1, LSystemInfo) total
188190 ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
189191 ++ concatMap vecOps vecTypes
190192 ++ concatMap fixedOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64] -- ITNative, ITChar, ATFloat ] ++ vecTypes