Skip to content

Instantly share code, notes, and snippets.

View ishikawa's full-sized avatar
🏠
Working from home

Takanori Ishikawa ishikawa

🏠
Working from home
View GitHub Profile
@ishikawa
ishikawa / llvm_ruby_ffi_callback.rb
Created May 17, 2014 23:54
Call Ruby method (FFI::Function) from LLVM
require 'ffi'
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
CALLBACK_TYPE = LLVM.Function([LLVM::Int], LLVM.Void)
mod = LLVM::Module.new("test.fprintf")
do_work = mod.functions.add("do_work", [CALLBACK_TYPE.pointer], LLVM.Void) do |fn, callback|
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
mod = LLVM::Module.new("test.fprintf")
str = "value = %d\n"
V_STRING = mod.globals.add(LLVM::Array(LLVM::Int8, str.size + 1), "str") do |value|
value.linkage = :internal
@ishikawa
ishikawa / llvm_puts.rb
Created May 17, 2014 13:40
Sample POSIX "Hello World" Application (ruby-llvm)
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
mod = LLVM::Module.new("test")
str = "Hello, World!"
STRING = mod.globals.add(LLVM::Array(LLVM::Int8, str.size + 1), "str") do |value|
value.linkage = :internal
# LLVM::JITCompiler#run_function can't handle aggregate value
engine = LLVM::JITCompiler.new(module)
function = module.functions["doSomething"]
pointer = engine.pointer_to_global(function)
fn = FFI::Function.new(:int, [:int], pointer)
fn.call(123)
@ishikawa
ishikawa / putd.c
Created May 4, 2014 02:23
How to link modules and JIT execution by using ruby-llvm
#include <stdio.h>
int putd(double x) {
printf("putd: %.2f\n", x);
return 1;
}
julia> @async begin
server = listen(2000)
while true
sock = accept(server)
println("Hello World\n")
end
end
Task (queued) @0x00007fe0d62ae100
julia> connect(2000)
@ishikawa
ishikawa / max.exs
Created December 21, 2013 14:10
Programming Elixir Exercise: ListsAndRecursion-2
# Programming Elixir
# Exercise: ListsAndRecursion-2
#
# Write max(list) that returns the element with the maximum
# value in the list. (This is slightly trickier than it sounds.)
#
# http://forums.pragprog.com/forums/322/topics/Exercise:%20ListsAndRecursion-2
defmodule Max do
def max([a]), do: a
! Copyright (C) 2013 Takanori Ishikawa.
! See http://factorcode.org/license.txt for BSD license.
USING: tools.test fizzbuzz ;
IN: fizzbuzz.tests
! divisible3?
[ t ] [ 0 divisible3? ] unit-test
[ f ] [ 1 divisible3? ] unit-test
[ t ] [ 3 divisible3? ] unit-test
@ishikawa
ishikawa / gist:5203419
Created March 20, 2013 09:28
How to compute Fixnum mask
FIXNUM_MASK = (0..64).map {|n| (1<<n)-1 }
.select {|m| m.instance_of?(Fixnum) }
.max
# irb(main):009:0> 10000000000000000000000.class
# => Bignum
# irb(main):010:0> (10000000000000000000000 & FIXNUM_MASK).class
# => Fixnum
; ModuleID = 'hello'
@.str = private constant [14 x i8] c"Hello, World!\00"
declare i32 @puts(i8* nocapture) nounwind
define i32 @main() {
%1 = call i32 @puts(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0))
ret i32 0
}