Skip to content

Instantly share code, notes, and snippets.

@ytomino
ytomino / cppexc.adb
Created May 20, 2014 01:46
throwing and catching a C++ exception
with Ada.Text_IO;
with Interfaces.C.Strings;
with GNAT.CPP_Exceptions;
procedure cppexc is
pragma Linker_Options ("-lstdc++");
use type Interfaces.C.char_array;
char_const_ptr : exception;
pragma Import (Cpp, char_const_ptr, "_ZTIPKc"); -- typeid(char const *)
begin
Ada.Text_IO.Put_Line ("throw...");
@ytomino
ytomino / dtmodoki.ads
Created July 30, 2014 14:43
コンパイル時に結果をチェックできる引き算
package dtmodoki is
-- minus functions ensuring Left >= Right (Left - Right >= 0)
generic
X : Natural;
Y : Natural;
package Natural_Minus is
subtype Result is Natural range X - Y .. X - Y;
@ytomino
ytomino / test1.ads
Last active August 29, 2015 14:04
SPARKで遊んでみた
pragma Annotate (GNATprove, External_Axiomatization);
package test1
with Pure, SPARK_Mode
is
function fact (x : Integer) return Integer is
(if x > 0 then
x * fact (x - 1) -- overflow check might fail
else
1);
@ytomino
ytomino / toplevelpp.ml
Last active August 29, 2015 14:05
Use the toplevel pretty-printer from source code
(* ocaml -I `ocamlc -where`/compiler-libs ocamlcommon.cma toplevelpp.ml *)
module L = Longident;;
let print_value long_id value =
let env = !Toploop.toplevel_env in
let value_path, value_desc = Env.lookup_value long_id env in
Toploop.print_value
env
(Obj.repr value)
extern inline int extinl (int x) {return x;}
inline int norinl (int x) {return x;}
static inline int stainl (int x) {return x;}
extern void dummy(int x);
void test(void)
{
dummy(extinl(1));
dummy(norinl(1));
@ytomino
ytomino / c-bsm-audit.ads
Last active August 29, 2015 14:16
i686-pc-freebsd7
-- This file is translated by "headmaster" version 0.26-a54347a (devel).
-- The original C header's license should be applied to this file.
-- All conditional-directives are expanded for the exclusive use of your
-- environment, it is not recommended to commit this file to any repository.
-------------------------------------------------------------------------------
with C.stddef;
with C.sys.types;
package C.bsm.audit is
pragma Preelaborate;
subtype au_id_t is sys.types.uid_t;
__attribute__((noinline)) int c_sum(int *data, int n)
{
int result = 0;
for(int i = 0; i < n; ++i){
result += data[i];
}
return result;
}
@ytomino
ytomino / urr.adb
Created March 6, 2011 07:13
Universal Representation of Real numbers
with Ada.Unchecked_Conversion;
with Ada.Numerics.Long_Long_Elementary_Functions;
with Interfaces;
package body URR is
function Shift_Left (Value : URR_Representation; Amount : Natural) return URR_Representation;
pragma Import (Intrinsic, Shift_Left);
function To_URR_Real (X : URR_Representation) return URR_Real is
begin
@ytomino
ytomino / difference.adb
Created March 6, 2011 07:53
diff algorithm
with Ada.Unchecked_Deallocation;
with System.Pool_Local;
package body Difference is
procedure Diff (
Left, Right : in Container_Type;
Notify : not null access procedure (
Left_Low : Index_Type; Left_High : Index_Type'Base;
Right_Low : Index_Type; Right_High : Index_Type'Base))
is