n7m2(!packetInCount - TWO)
macdef packetIn = $extval(arrayref(uint8, 127),"packetIn")
macdef packetInCount = $extval(ref(uint8),"packetInCount")
macdef packetInHeader = $extval(ref(uint8),"packetInHeader")
extern fun{} n7 (x: uint8): natLte(127)implement{} n7 (x) = cast{natLte(127)}(x land u8(0x7F))
extern fun{} n7m1 (x: uint8): natLte(126)implement{} n7m1 (x) = if x > u8(126) then cast{natLte(126)}(126) else cast{natLte(126)}(x)
extern fun{} n7m2 (x: uint8): natLte(125)implement{} n7m2 (x) = if x > u8(125) then cast{natLte(125)}(125) else cast{natLte(125)}(x)
fun read_frame(): bool = let val () = !packetInCount := serial_poll_for_byte() // Discarded the multi-frame bit val ok = if !packetInCount >= THREE // Three is the minimum packet size other than zero. then let fun loop {n: nat | n <= 125} .<n>. (cnt: int(n), cksum: uint8): uint8 = if cnt > 0 then let val byte = serial_poll_for_byte() val () = packetIn[n7m2(!packetInCount - u8(cnt) - ONE)] := byte in loop (cnt - 1, pec_add(cksum, byte)) end else cksum val cksum = loop (n7m2(!packetInCount - TWO), pec_add(ZERO, !packetInCount)) val packetInChecksum = serial_poll_for_byte() val () = packetIn[n7m1(!packetInCount - ONE)] := packetInChecksum in cksum = packetInChecksum end else false in ok end
--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/C711F7E3-0BC5-4768-AC0E-7A44C77333C3%40gmail.com.
fun read_frame(): bool = let val () = !packetInCount := serial_poll_for_byte() fun read {n0: nat | n0 <= 127} (count: int(n0)): bool = if count >= 3 then let fun loop {n: nat | n0 - 2 >= n} .<n>. (cnt: int(n), cksum: uint8): uint8 = if cnt > 0 then let val byte = serial_poll_for_byte() val () = packetIn[count - cnt - 1] := byte in loop (cnt - 1, pec_add(cksum, byte)) end else cksum val cksum = loop (count - 2, pec_add(ZERO, u8(count))) val packetInChecksum = serial_poll_for_byte() val () = packetIn[count - 1] := packetInChecksum in cksum = packetInChecksum end else false in read(n7(!packetInCount)) end--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/dfb4f081-2442-45c5-8a9c-4e06c68647ee%40googlegroups.com.
$UN.cast{uint8}(x) is translated to ((uint8)(x)) in C, which I think is just a form of clamping.For every n, int(n) and int have the same representation.Types like int8 and uint8 are mostly used to classify components of a struct or array. For classifyingvariables, please use int_i8 and int_u8:typedef int_i8 = intBtw(~128, 127) and int_u8 = intBtw(0, 255)Using int_i8 and int_u8 can also let you discover potential overflows.I don't know much about MISRA, but I am pretty certain that you can do a lot better with types like int_i8 and int_u8 :)Cheers!
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
I think the MISRA approach is to add constraints to the C code, with tools to enforce them. All violations must be fixed or documented. Practically, this means embedded code has some amount of documentation.The difficulty for ATS is that if a MISRA tool is run on the generated C code, it may not be easy to translate it back to ATS to fix, etc. I suppose a set of constraints on ATS could generate MISRA compliant code, but that is a lot of effort, and probably an entirely new research project.
that said, if there were an ATS compiler and prelude that prevented most violations except those that are unavoidable, like interrupt handling code, and if it were proven to be such, it would offer an alternative to direct MISRA C programming.Whether the market would accept this or not I do not know. This is just my pondering the possible.
fun set_n {n0: nat | n0 <= 127} (count: int(n0)): void = let fun loop {n: nat | n0 - 1 >= n} .<n>. (cnt: int(n), p: uint8): uint8 = if cnt > 0 then loop(cnt - 1, pec_add(ZERO, p)) else p in packetOut[count - 1] := loop(count - 1, ZERO) end--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/946c92b4-5ca5-4f36-aaaa-f58b10fa8d8f%40googlegroups.com.
extern fun{} n73 {n: nat | n >= 3 && n <= 127} (x: int): int(n)implement{} n73 (x) = if x < 3 then 3 else if x > 127 then 127 else x
fun set_n {n0: nat | n0 >= 3 && n0 <= 127} (count: int(n0)): void = let
typedef g1uintBtwe2 (tk:tk, lb0:int, ub0:int, lb1:int, ub1:int) = [i: int | lb0 <= i; i <= ub0; lb1 <= i; i <= ub1] g1uint (tk, i)typedef uintBtwe2 (lb0:int, ub0:int, lb1:int, ub1:int) = g1uintBtwe2 (uint_kind, lb0, ub0, lb1, ub1)
typedef hex_t = uintBtwe2(0x30, 0x39, 0x41, 0x46)
fun uint82hex (i: uint8) : hex_t = let val nv = if ((i >= u8(0x30) && i <= u8(0x39)) || (i >= u8(0x41) && i <= u8(0x46))) then i else u8(0x30)in cast{hex_t}(nv)endextern fun{} n73 (x: uint8): [n: int | n >= 3 && n <= 127] int(n)staload "prelude/SATS/integer.sats"
val ok = u8(0)
val OK = u8(1)
val b = if ok <> OK ...
There is a misuse of quantifier here:
extern fun{} n73 {n: nat | n >= 3 && n <= 127} (x: int): int(n)
What you need istypedef g1uintBtwe2
(tk:tk, lb0:int, ub0:int, lb1:int, ub1:int) = [i: int | lb0 <= i; i <= ub0; lb1 <= i; i <= ub1] g1uint (tk, i)
typedef g1uintBtwe2
(tk:tk, lb0:int, ub0:int, lb1:int, ub1:int) = [i: int | (lb0 <= i && i <= ub0) || (lb1 <= i && i <= ub1)] g1uint (tk, i)typedef
intsub(p:int->bool) = [i:int | p(i)] int(i)
stadef
intBtwe(i:int, j:int) =
lam (a:int): bool => (i <= a && a <= j)
typedef
intBtwe(i:int, j:int) = intsub(intBtwe(i, j))
typedef
intBtwe2(i1:int, j1:int, i2:int, j2:int) =
intsub(lam(a) => intBtwe(i1, j1)(a) || intBtwe(i2, j2)(a))
typedef hex = intBtwe2(0x30, 0x39, 0x41, 0x46)
fun
int2hex
(i:int): hex = let
val i = g1ofg0(i)
val _0_ = 0x30 and _9_ = 0x39
val _a_ = 0x41 and _f_ = 0x46
in
if i < _a_
then _0_
else (
if i > _9_
then (if i < _a_ then _0_ else (if i > _f_ then _0_ else i)) else i
// end of [if]
) (* else *)
end // end of [int2hex]
#include "config.hats"#include "{$TOP}/avr_prelude/kernel_staload.hats"staload "prelude/SATS/bool.sats"staload CH = "prelude/SATS/char.sats"staload "prelude/SATS/unsafe.sats"
extern fun{} n73 (x: uint8): [n: int | n >= 3; n <= 127] int(n)implement{} n73 (x) = if x < 3 then 3 else if x > 127 then 127 else cast{intBtwe(3,127)}(x)val v = n73(FOUR)
In file included from DATS/main_dats.c:15:0:DATS/main_dats.c: In function ‘ATSLIB_056_prelude__lt_g0uint_int__211__1’:DATS/main_dats.c:18821:23: error: ‘PMVtmpltcstmat’ undeclared (first use in this function) ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ; ^/opt/ATS/ATS2-Postiats-0.2.3/ccomp/runtime/pats_ccomp_instrset.h:266:37: note: in definition of macro ‘ATSINSmove’ #define ATSINSmove(tmp, val) (tmp = val) ^DATS/main_dats.c:18821:23: note: each undeclared identifier is reported only once for each function it appears in ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ; ^/opt/ATS/ATS2-Postiats-0.2.3/ccomp/runtime/pats_ccomp_instrset.h:266:37: note: in definition of macro ‘ATSINSmove’ #define ATSINSmove(tmp, val) (tmp = val) ^DATS/main_dats.c:18821:41: error: ‘g0int2uint’ undeclared (first use in this function) ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ; ^/opt/ATS/ATS2-Postiats-0.2.3/ccomp/runtime/pats_ccomp_instrset.h:266:37: note: in definition of macro ‘ATSINSmove’ #define ATSINSmove(tmp, val) (tmp = val) ^DATS/main_dats.c:18821:1: warning: implicit declaration of function ‘S2Eextkind’ [-Wimplicit-function-declaration] ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ; ^In file included from DATS/main_dats.c:15:0:DATS/main_dats.c:18821:63: error: expected expression before ‘atstype_int’ ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ; ^/opt/ATS/ATS2-Postiats-0.2.3/ccomp/runtime/pats_ccomp_instrset.h:266:37: note: in definition of macro ‘ATSINSmove’ #define ATSINSmove(tmp, val) (tmp = val) ^DATS/main_dats.c:18821:88: error: expected expression before ‘atstype_uint8’ ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ;
ATSstatic()/*imparg = tk(4495)tmparg = S2Evar(tk(4495))tmpsub = Some(tk(4495) -> S2Eextkind(atstype_uint8))*/atstkind_t0ype(atstype_bool)ATSLIB_056_prelude__lt_g0uint_int__211__1(atstkind_t0ype(atstype_uint8) arg0, atstkind_t0ype(atstype_int) arg1){/* tmpvardeclst(beg) */ATStmpdec(tmpret720__1, atstkind_t0ype(atstype_bool)) ;ATStmpdec(tmp721__1, atstkind_t0ype(atstype_uint8)) ;/* tmpvardeclst(end) */ATSfunbody_beg()/*emit_instr: loc0 = /opt/ATS/ATS2-Postiats-0.2.3/prelude/DATS/integer.dats: 30626(line=879, offs=1) -- 30682(line=879, offs=57)*/ATSINSflab(__patsflab_lt_g0uint_int):/*emit_instr: loc0 = /opt/ATS/ATS2-Postiats-0.2.3/prelude/DATS/integer.dats: 30667(line=879, offs=42) -- 30680(line=879, offs=55)*/ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ;
/*emit_instr: loc0 = /opt/ATS/ATS2-Postiats-0.2.3/prelude/DATS/integer.dats: 30649(line=879, offs=24) -- 30682(line=879, offs=57)*/ATSINSmove(tmpret720__1, atspre_g0uint_lt_uint8(arg0, tmp721__1)) ;
ATSfunbody_end()ATSreturn(tmpret720__1) ;} /* end of [ATSLIB_056_prelude__lt_g0uint_int__211__1] *//*emit_instr: loc0 = /opt/ATS/ATS2-Postiats-0.2.3/prelude/DATS/integer.dats: 30667(line=879, offs=42) -- 30680(line=879, offs=55)*/ATSINSmove(tmp721__1, PMVtmpltcstmat[0](g0int2uint<S2Eextkind(atstype_int), S2Eextkind(atstype_uint8)>)(arg1)) ;
--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/1cd4da0e-0860-49b6-bc0d-81805744b00b%40googlegroups.com.